dsj1961061 2012-06-20 10:21
浏览 66
已采纳

spl_autoload_register无法在包含的文件中工作

I'm using spl_autoload_register in order to load classes.

I have an index.php file that includes an init.php file. The spl_autoload_register function is called in the init.php file.

In the index.php file, it works properly: I can create classes and stuff, their names are resolved.

But later on, in the index.php, I include another file, work.php to do some specific task.

Oddly, in the work.php, the class I'm using are not found.

If I call the spl_autoload_register once again in work.php, the class can then be resolved.

The really odd thing about it is that this behavior is not consistent: On my test server, I don't have to duplicate the spl_autoload_register call, but on my production server, it's compulsory.

Am I missing some options in the php.ini?

Edit/Updated: Here is the content on the init.php file, just in case:

<?php
function my_autoload($class){
    include 'class/' . $class . '.class.php';
}

spl_autoload_register('my_autoload');
?>

And my index.php :

<?php
require_once 'include/init.php';

$barcode = new Barcode();
// here is a bunch of test and stuff
include 'work.php';
?>

And my work.php :

<?php
$myObj = new Barcode();
// more useles stuff
?>

The barcode is created flawlessly in the index.php code part, but fails in the work.php part...

  • 写回答

2条回答 默认 最新

  • doujuanqi2909 2012-06-20 10:32
    关注

    Check your include_path setting if it contains the local directory.

    Generally it is not a good idea to use relative paths. It's easy to create an absolute path that's relative to your init.php file like this:

    function my_autoload($class){
        include __DIR__.'/class/' . $class . '.class.php';
    }
    

    This code assumes that your init.php file is in the same folder as the 'class' folder.

    Also, instead of potentially creating errors with blindly including files that may or may not exist in the given path, check the existence of the file before including it:

    function my_autoload($class){
        $file = __DIR__.'/class/' . $class . '.class.php';
        if(file_exists($file)) {
           include $file;
        }
    }
    

    Please note that __DIR__ is a PHP 5.3 feature which is not be available on hosts with PHP 5.2. You can substitute it with dirname(__FILE__)

    Also, please note that on Linux the file is searched for in a case-sensitive manner, whole on most Mac OS X install, the file is looked for case-insensitively. If you instantiate class MyClass Linux will look for MyClass.class.php while Mac OS X would also load the class if it was in file called myclass.class.php, Myclass.class.php, etc.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP