2

我在 PHP 的魔术__sleep()方法中遇到了自动加载类的问题。不会发生自动加载,因此无法找到该类。为了调试它,我尝试调用spl_autoload_functions()它然后导致 PHP 出现段错误......

下面的示例代码演示了该问题。使用实例方法或静态方法具有相同的行为。这对我来说似乎很好用__destruct(),这很适合我的用例,但我很好奇这背后的原因。它是一个 PHP 错误,还是有一个合理的解释?

Foo.php, 就像一个自动加载目标

<?php
class Foo {
    public static function bar() {
        echo __FUNCTION__;
    }
}
?>

testcase.php

<?php
class Autoloader {
    public static function register() {
        // Switch these calls around to use a static or instance autoload function
        spl_autoload_register('Autoloader::staticLoad');
        //spl_autoload_register(array(new self, 'instanceLoad'));
    }

    public function instanceLoad($class) {
        require_once dirname(__FILE__) . '/' . $class . '.php';
    }

    public static function staticLoad($class) {
        require_once dirname(__FILE__) . '/' . $class . '.php';
    }
}
Autoloader::register();

class Bar {
    public function __sleep() {
        // Uncomment the next line to segfault php...
        // print_r(spl_autoload_functions());
        Foo::bar();
    }
}
$bar = new Bar;

这可以通过将两个文件放在一个目录中并运行php testcase.php. 这发生在我的 PHP 5.3.3 和 5.2.10 中。

4

1 回答 1

1

您描述的问题听起来与 PHP 错误跟踪器中的此条目非常相似:
http ://bugs.php.net/bug.php?id=53141

该错误已在 PHP 5.3.4 中修复(在http://php.net/ChangeLog-5.php上搜索“53141” )。

于 2011-05-20T01:49:30.367 回答