php include_path设置

 
   

一般情况下,我们设置php的include_path都会通过修改php.ini来实现。
有时候,我们没有服务器的权限。有时候,我们把一个目录加到include_path会让已有的程序冲突。受cakephp的启发:在app/webroot目录下index.php有如下代码

ini_set('include_path', CAKE_CORE_INCLUDE_PATH . PATH_SEPARATOR . ROOT . DS . APP_DIR . DS . PATH_SEPARATOR . ini_get('include_path'));


我们看到这个程序动态修改include_path。不过cake在这儿是把 CAKE_CORE_INCLUDE_PATH 和 APP_DIR 加到 include_path里,并且优先在这两个目录下找包含程序。
注意到它这里用到了PATH_SEPARATOR这个变量。这样这段代码在windows和linux下能通用。

从中受到启发,我们可以根据自己的需要把一些include目录动态的加入进来。比如说我们有很多libs:lib1,lib2,lib3等等。我们不必把这些libs都加到include_path里,因为它们之间可能冲突。
可以建立一个inc_dir,并把这个目录加入到include_path。在inc_dir下,分别建立inc_path1.php inc_path2.php inc_path3.php
分别写入

<?php//set_include_path('.'.PATH_SEPARATOR.get_include_path().PATH_SEPARATOR.'E:/DQG/inc');
ini_set('include_path', ini_get('include_path').PATH_SEPARATOR.$dirToLib1);
<?php
ini_set('include_path', ini_get('include_path').PATH_SEPARATOR.$dirToLib2);
<?php
ini_set('include_path', ini_get('include_path').PATH_SEPARATOR.$dirToLib3);

在写程序的时候,比如要用lib2的functions.php
就可以这么写

Code
<?php
require 'inc_path2.php';
require 'functions.php';
//.

转载于:https://www.cnblogs.com/200831856/archive/2008/11/28/1343469.html

猜你喜欢

转载自blog.csdn.net/weixin_34080571/article/details/93711122