将PHP__autoload与Mollie_API_Autoloader类结合使用

时间:2017-11-16 09:09:42

标签: php autoload mollie

虽然我知道要求使用即用型代码是不合适的,但在这种情况下,我确实没有找到解决方案。

尝试使用本机PHP __autoload函数以及Mollie_API_Autoloader类:

class Mollie_API_Autoloader
{
/**
 * @param string $class_name
 */
public static function autoload ($class_name)
{
    if (strpos($class_name, "Mollie_") === 0)
    {
        $file_name = str_replace("_", "/", $class_name);
        $file_name = realpath(dirname(__FILE__) . 
"/../../{$file_name}.php");

        if ($file_name !== false)
        {
            require $file_name;
        }
    }
}

/**
 * @return bool
 */
public static function register ()
{
    return spl_autoload_register(array(__CLASS__, "autoload"));
}

/**
 * @return bool
 */
public static function unregister ()
{
    return spl_autoload_unregister(array(__CLASS__, "autoload"));
}
}

Mollie_API_Autoloader::register();

Mollie自动加载器类会影响本机PHP __autoload类的正常运行。似乎无法将这些结合起来。我是怎么做到的?

1 个答案:

答案 0 :(得分:0)

__autoload()已被弃用,多年前被spl_autoloader_register()取代(在PHP 5.1.2上)。

他们无法一起工作。 __autoload()是有限的,只有spl_autoloader_register()做得更好。

由于您发布的课程已使用spl_autoload_register(),您所要做的就是致电

Mollie_API_Autoloader::register()

你的boostrap代码中的某个地方。

如果现有代码使用__autoload(),您可以轻松将其更改为使用spl_autoload_register()。只需将__autoload函数重命名为其他内容(让我们说old_autoloader)并在定义后添加spl_autoload_register('old_autoloader');

查看Example #1文档中的spl_autoload_register()以获取示例。