set_include_path()和get_include_path() 这个两个方法怎么用啊?
老师能不能详细讲解下set_include_path()和get_include_path() 这个两个方法怎么用啊?PATH_SEPARATOR是什么,百度了一天了都看不懂,这方面的学习资料好少啊
老师能不能详细讲解下set_include_path()和get_include_path() 这个两个方法怎么用啊?PATH_SEPARATOR是什么,百度了一天了都看不懂,这方面的学习资料好少啊
2016-10-29
先跟你解释下 include_path是包含路径的意思,也就是说如果你设置了包含路径,那么在include( )或者require()时会优先在你设置的路径下寻找文件,如果没有设置,那么就必须在include()或者require()时在( )里面写“路径/文件名”,所以如果经常要用include()或者require()函数的话,为了避免每次都写“路径/文件名”,我就先设置好包含路径,这样就不要每次都写了;
那么怎么设置包含路径呢,当然是用set_include_path()了;
同理,get_include_path()就是获得包含路径的意思。
PATH_SEPARATOR是路径分隔符,在window下用分号表示。
下面结合老师的例子分析:
<?php header("content-type:text/html;charset=utf-8"); session_start(); define('ROOT', dirname(__FILE__)); set_include_path('.'.PATH_SEPARATOR.ROOT.'/lib'.PATH_SEPARATOR.ROOT.'/configs'.PATH_SEPARATOR.ROOT.'/core'.PATH_SEPARATOR.get_include_path()); echo '.'.PATH_SEPARATOR.ROOT.'/lib'.PATH_SEPARATOR.ROOT.'/configs'.PATH_SEPARATOR.ROOT.'/core'.PATH_SEPARATOR.get_include_path(); //下面就是用set_include_path()设置的包含路径,我把它echo出来了: //.;D:\wamp\www\shopImooc1\shopImooc/lib;D:\wamp\www\shopImooc1\shopImooc/configs;D:\wamp\www\shopImooc1\shopImooc/core;.;D:\wamp\www\shopImooc1\shopImooc/lib;D:\wamp\www\shopImooc1\shopImooc/configs;D:\wamp\www\shopImooc1\shopImooc/core;.;C:\php\pear echo "<hr>"; echo get_include_path(); //这是get_include_path()的路径,如下: //.;D:\wamp\www\shopImooc1\shopImooc/lib;D:\wamp\www\shopImooc1\shopImooc/configs;D:\wamp\www\shopImooc1\shopImooc/core;.;C:\php\pear require_once 'string.func.php'; require_once 'image.func.php'; require_once 'upload.func.php'; require_once 'page.func.php'; require_once 'mysql.func.php'; require_once 'common.func.php'; require_once 'configs.php'; require_once 'admin.inc.php'; require_once 'cate.inc.php'; connect();
注意看我上面代码里的注释,你也可以自己打印出来看看。在set_include_path( )里,最好把get_include_path写在前面,像这样:set_include_path(get_include_path().PATH_SEPARATOR . ROOT.'/core');
.;D:\wamp\www\shopImooc1\shopImooc/lib;D:\wamp\www\shopImooc1\shopImooc/configs;D:\wamp\www\shopImooc1\shopImooc/core;.;C:\php\pear ----------------------------这段路径里黑体的分号就叫: PATH_SEPARATOR . ;C:\php\pear是php默认的include_path
PS: 说的有点啰嗦,不知道说清楚了没有,O(∩_∩)O哈哈~
举报