【问题标题】:__autoload() not being called?__autoload() 没有被调用?
【发布时间】:2014-10-16 08:05:06
【问题描述】:

我正在使用 Amazon Payments PHP SDK,并且 __autoload() 在浏览器中运行良好,但是当我切换到我的 CLI 脚本时,它似乎并没有调用该函数。

我得到的只是“PHP 致命错误:找不到类 'OffAmazonPaymentsService_Client'”。

我已经在 __autoload() 函数中进行了调试,以回显被调用的函数和文件路径,并且终端中没有打印任何内容,只是在浏览器中。

我已经完成了 print_r(get_defined_functions());并且 __autoload() 列在它所在文件的 require_once() 之后,之前没有列出,所以我知道它得到了正确的功能。

我还检查了被设置的 include_path,它位于 Amazon Payments 源文件夹的根目录中,它应该在其中,所以如果调用 __autoload(),它没有理由找不到类 OffAmazonPaymentsService_Client。

谁能告诉我为什么 __autoload() 在 CLI 中不起作用?我没有执行 php -a...

【问题讨论】:

  • 服务器 php.ini 和 cli php.ini 是否“相同”,即相同的包含路径/模块等。
  • 是的,PHP 脚本覆盖了很多内容,特别是包含路径。但是,对于服务器/浏览器脚本和 CLI 脚本,都进行了相同的 include_path 更改。本质上,它们是相同的。
  • 启动脚本的确切命令是什么?
  • 你可以尝试运行 php -f myscript.php

标签: php linux command-line-interface amazon-pay


【解决方案1】:

我已将 AmazonPayments PHP SDK 中的 __autoload() 替换为 spl_autoload_register() 并且有效。

/*
function __autoload($className){
        $filePath = str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
        $includePaths = explode(PATH_SEPARATOR, get_include_path());
        foreach($includePaths as $includePath){
                if(file_exists($includePath . DIRECTORY_SEPARATOR . $filePath)){
                        require_once $filePath;
                        return;
                }
        }
}
*/

spl_autoload_register(function($className){
        $filePath = str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
        $includePaths = explode(PATH_SEPARATOR, get_include_path());
        foreach($includePaths as $includePath){
                if(file_exists($includePath . DIRECTORY_SEPARATOR . $filePath)){
                        require_once $filePath;
                        return;
                }
        }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-15
    • 1970-01-01
    • 1970-01-01
    • 2018-11-25
    • 2019-03-31
    • 2012-12-16
    • 2015-03-28
    相关资源
    最近更新 更多