php 自动加载类 设置包含目录 随便new也不出错

发布时间:2013-9-28 18:13    发布者:reggae
关键词: php
get_include_path — 获取当前的 include_path 配置选项

string get_include_path ( void )

set_include_path — 设置 include_path 配置选项
string set_include_path ( string $new_include_path )

首先set_include_path这个函数呢,是在脚本里动态地对PHP.ini中include_path进行修改的。
而这个include_path呢,它可以针对下面的include和require的路径范围进行限定,或者说是预定义一下。
就好像:
如果我们没有设置这个值,可能我们需要写一些完全的路径:

[php]
  1.    include("123/test1.php");
  2.    include("123/test2.php");
  3.    include("123/test3.php");
  4.    require("123/test4.php");
  5.    require("123/test5.php");
  6. ?>

  7.       
  8.           include("123/test1.php");
  9.           include("123/test2.php");
  10.           include("123/test3.php");
  11.           require("123/test4.php");
  12.           require("123/test5.php");
  13.        ?>
复制代码

来引入很多外部文件,但是如果我们设置了set_include_path("123/"),我们就可以用下面这段代码代替。
[php]
  1.    set_include_path("123/");
  2.    include("test1.php");
  3.    include("test2.php");
  4.    include("test3.php");
  5.    require("test4.php");
  6.    require("test5.php");
  7. ?>

  8.       
  9.           set_include_path("123/");
  10.           include("test1.php");
  11.           include("test2.php");
  12.           include("test3.php");
  13.           require("test4.php");
  14.           require("test5.php");
  15.        ?>    那么这个函数它不仅可以定义一个文件夹,我们可以定义很多文件夹。如下所示,我要写一个初始化函数:
  16. [php]
  17.   function initialize()

  18. set_include_path(get_include_path().PATH_SEPARATOR . "core/");
  19. set_include_path(get_include_path().PATH_SEPARATOR . "app/");
  20. set_include_path(get_include_path().PATH_SEPARATOR . "admin/");
  21. set_include_path(get_include_path().PATH_SEPARATOR . "lib/");
  22. set_include_path(get_include_path().PATH_SEPARATOR . "include/");
  23. set_include_path(get_include_path().PATH_SEPARATOR."data/");
  24. set_include_path(get_include_path().PATH_SEPARATOR."cache/");

  25.        function initialize()
  26. {
  27.     set_include_path(get_include_path().PATH_SEPARATOR . "core/");
  28.     set_include_path(get_include_path().PATH_SEPARATOR . "app/");
  29.     set_include_path(get_include_path().PATH_SEPARATOR . "admin/");
  30.     set_include_path(get_include_path().PATH_SEPARATOR . "lib/");
  31.     set_include_path(get_include_path().PATH_SEPARATOR . "include/");
  32.     set_include_path(get_include_path().PATH_SEPARATOR."data/");
  33.     set_include_path(get_include_path().PATH_SEPARATOR."cache/");
  34. }   
复制代码

这样它的路径就成了:




.;C:\php5\pear;core/;app/;admin/;lib/;include/;data/;cache/


下面呢来一个实例.

[php]
  1. $include_path=get_include_path();                         //原基目录  
  2. $include_path.=PATH_SEPARATOR."include/" ;
  3. $include_path.=PATH_SEPARATOR."classs/";
  4. $include_path.=PATH_SEPARATOR."libs/";
  5. //echo $include_path;  
  6. //设置include包含文件所在的所有目录   
  7. set_include_path($include_path);

  8. function __autoload($className)
  9. {
  10. //echo '类 '.$className;  
  11. include strtolower($className).".class.php";
  12. }
  13. $Smarty = new Smarty;
  14. ?>

  15. $include_path=get_include_path();                         //原基目录
  16. $include_path.=PATH_SEPARATOR."include/" ;
  17. $include_path.=PATH_SEPARATOR."classs/";
  18. $include_path.=PATH_SEPARATOR."libs/";
  19. //echo $include_path;
  20. //设置include包含文件所在的所有目录
  21. set_include_path($include_path);

  22. function __autoload($className)
  23. {
  24. //echo '类 '.$className;
  25. include strtolower($className).".class.php";
  26. }
  27. $Smarty = new Smarty;
  28. ?>
复制代码

当指定了多个目录为 include_path ,而所要求包含的文件在这几个目录都有相同名称的文件存在时,php选择使用设定 include_path 时排位居前的目录下的文件。

这样就可以 直接new拉!!

本文地址:https://www.eechina.com/thread-121475-1-1.html     【打印本页】

本站部分文章为转载或网友发布,目的在于传递和分享信息,并不代表本网赞同其观点和对其真实性负责;文章版权归原作者及原出处所有,如涉及作品内容、版权和其它问题,我们将根据著作权人的要求,第一时间更正或删除。
您需要登录后才可以发表评论 登录 | 立即注册

厂商推荐

关于我们  -  服务条款  -  使用指南  -  站点地图  -  友情链接  -  联系我们
电子工程网 © 版权所有   京ICP备16069177号 | 京公网安备11010502021702
快速回复 返回顶部 返回列表