开发者

PHP autloading failing in __sleep() magic method

开发者 https://www.devze.com 2023-03-07 16:39 出处:网络
I\'m having issues with autoloading classes in PHP\'s magic __sleep() method.Autoloading doesn\'t take place, so the class cannot be found.In an attempt to debug this I tried calling spl_autoload_func

I'm having issues with autoloading classes in PHP's magic __sleep() method. Autoloading doesn't take place, so the class cannot be found. In an attempt to debug this I tried calling spl_autoload_functions() which then causes PHP to segfault...

The example code below demonstrates the problem. Using an instance method or a static method has the same behaviour. This seems to work fine for me using __destruct() instead, which suits my use case fine, but I'm curious as to the reason behind this. Is it a PHP bug, or is there a sensible explanation?

In Foo.php, just as an autoload target

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

In 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;

This can be run by placing both files in a directory and running php testcase.php. This occurs for me with PHP 5.3.3 and 5.2.10.


The problem you describe sounds very similar to this entry in the PHP bug tracker:
http://bugs.php.net/bug.php?id=53141

That bug was fixed in PHP 5.3.4 (search for "53141" on http://php.net/ChangeLog-5.php).

0

精彩评论

暂无评论...
验证码 换一张
取 消