为了账号安全,请及时绑定邮箱和手机立即绑定

set_include_path()和get_include_path() 这个两个方法怎么用啊?

老师能不能详细讲解下set_include_path()和get_include_path() 这个两个方法怎么用啊?PATH_SEPARATOR是什么,百度了一天了都看不懂,这方面的学习资料好少啊

正在回答

1 回答

先跟你解释下 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哈哈~




4 回复 有任何疑惑可以回复我~
#1

superhuman进化中 提问者

非常感谢!
2016-11-06 回复 有任何疑惑可以回复我~
#2

一亩地

不客气!
2016-11-08 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
手把手教你实现电商网站后台开发
  • 参与学习       117247    人
  • 解答问题       2101    个

手把手教你用PHP搭建电子商务平台,由浅入深教你搭建电商系统

进入课程

set_include_path()和get_include_path() 这个两个方法怎么用啊?

我要回答 关注问题
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号