doujiao4710 2010-04-22 15:26
浏览 33
已采纳

PHP对象中的文件引用如何工作?

I'm trying to create an PHP object that can load objects in other files on demand when needed. My problem is that when I reference the files based on file location for the class definition, it can not find the files. So file structure:

/Test.php

/os/os.php (extends kernel)

/os/kernel.php

/os/libraries/lib1.php

/os/libraries/lib2.php

/os/libraries/lib3.php

In kernel.php, the libraries are referenced as 'libraries/lib1.php'. If I create an "os" object in Test.php. The lib files are not found.

  • 写回答

2条回答 默认 最新

  • doulan8152 2010-04-22 15:50
    关注

    Use a good naming convention for your classes, include_path and use spl_autoload.

    For exemple:

    Name your classes like this: class Os class Kernel class Libraries_Lib1 ...

    Register your include path

    set_include_path(implode(PATH_SEPARATOR, array(
        realpath('os/'),
        get_include_path(),
    )));
    spl_autoload_register("autoload");
    

    And use an autoload:

    function autoload($className) {
       $name = implode("/", explode("_", $className));
       require_once(strtolower($name) . ".php");
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大