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条)

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错