我对我的实体使用 JsonSerialized 将它们转换为 json 数组,如下所示:
namespace backoffice\entities;
class MyObject implements \JsonSerializable{
//---do some stuff---
}
然后我使用 Autoloader.php 按命名空间加载类:
/**
* Class Autoloader
*/
class Autoloader{
static function register(){
spl_autoload_register(array(__CLASS__, 'autoload'));
}
static function autoload($className){
$className = str_replace('\\', DIRECTORY_SEPARATOR, $className);
require_once $className.'.php';
}
}
在autoload
函数中,我将\\
替换为服务器的directory_separator。
自动加载器毫无问题地加载我的类,但是当他来到 JsonSerialized (这是一个 SPL 接口(interface))时,转换 \\
并从我的命名空间(他找不到它)加载它,并向我显示此错误:
Warning: require_once(backoffice/entities/JsonSerializable.php) [function.require-once]: failed to open stream: No such file or directory
如何让它正常工作并从SPL加载这个接口(interface)?
最佳答案
我找到了答案。
这是一个 PHP 版本问题,JsonSerialized 需要 PHP 5.4,而在线服务器使用 PHP 5.3,所以这就是为什么他找不到它,它在本地主机上工作,因为我使用的是 PHP 5.5
关于PHP:自动加载找不到 JsonSerialized,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35522034/