php include_path设置

WBOY
Release: 2016-06-23 14:35:22
Original
1170 people have browsed it

 

 

Copy after login

一般情况下,我们设置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'));
Copy after login


我们看到这个程序动态修改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);<?phpini_set('include_path', ini_get('include_path').PATH_SEPARATOR.$dirToLib2);<?phpini_set('include_path', ini_get('include_path').PATH_SEPARATOR.$dirToLib3);
Copy after login

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

Code      <?phprequire 'inc_path2.php';require 'functions.php';//.
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!