这种通过有点想path方式的包含文件比较适用,php include_path设置

jackxiang 2008-8-27 17:56 | |
php include_path设置2008年05月05日 星期一 下午 07:18一般情况下,我们设置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
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
就可以这么写
<?php
require 'inc_path2.php';
require 'functions.php';
//....




<?php
define("SERVER_ROOT",dirname(__FILE__));

function init_www()
{
        if(PHP_OS=='WINNT') {
                $xg     = "\\";
                $fg     = ";";
        }else{
                $xg     = "/";
                $fg     = ":";
        }
        $need_include = array(

                SERVER_ROOT . "{$xg}admin{$xg}operation{$xg}",
                SERVER_ROOT ,
                SERVER_ROOT . "{$xg}libs{$xg}",
                dirname(SERVER_ROOT) . "{$xg}libs{$xg}",
        );
        $include_path = explode($fg,ini_get('include_path'));
        foreach($need_include as $path)
        {
                if(!in_array($path,$include_path))
                {
                        $include_path[] = $path;
                }
        }
        ini_set('include_path',join($fg,$include_path));
}
init_www();
include_once('config/commstr.php');
[xiangdong2@vm19 admin]$ vi /data1/www/htdocs/app.space.sina.com.cn/starcxc/stdafx.php

        ini_set('include_path',join($fg,$include_path));
<?php
define("SERVER_ROOT",dirname(__FILE__));

function init_www()
{
        if(PHP_OS=='WINNT') {
                $xg     = "\\";
                $fg     = ";";
        }else{
                $xg     = "/";
                $fg     = ":";
<?php
define("SERVER_ROOT",dirname(__FILE__));

function init_www()
{
        if(PHP_OS=='WINNT') {
                $xg     = "\\";
                $fg     = ";";
        }else{
                $xg     = "/";
                $fg     = ":";
        }
        $need_include = array(

                SERVER_ROOT . "{$xg}admin{$xg}operation{$xg}",
                SERVER_ROOT ,
                SERVER_ROOT . "{$xg}libs{$xg}",
                dirname(SERVER_ROOT) . "{$xg}libs{$xg}",
        );
        $include_path = explode($fg,ini_get('include_path'));
        foreach($need_include as $path)
        {
                if(!in_array($path,$include_path))
                {
                        $include_path[] = $path;
                }
        }
        ini_set('include_path',join($fg,$include_path));
}
init_www();
include_once('config/commstr.php');
include_once('config/db_config.php');

include_once('clsfactory.class.php');
include_once('page.class.php');
include_once('mysqlpdo.class.php');
include_once('user.class.php');
include_once('Json.php');
include_once('interface.class.php');
include_once('mysmarty.class.php');
include_once('adminsmarty.class.php');
?>

作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/1210/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!


最后编辑: jackxiang 编辑于2008-8-27 18:06
评论列表
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]