PHP简易博客报错:无法重新声明spl_autoload_register()函数
PHP迁移后替换__autoload为spl_autoload_register报错的解决方法
你出错的核心原因是把自定义自动加载函数命名成了spl_autoload_register——这是PHP自带的系统函数,重名会直接引发函数冲突错误。
直接按下面的步骤修改代码即可解决:
- 给自定义自动加载函数换一个不会和系统函数冲突的名字,比如
my_autoloader - 调用PHP内置的
spl_autoload_register,把你的自定义函数注册进去
修正后的完整代码:
//load classes as needed function my_autoloader($class) { $class = strtolower($class); //if call from within assets adjust the path $classpath = 'classes/class.'.$class . '.php'; if ( file_exists($classpath)) { require_once $classpath; } //if call from within admin adjust the path $classpath = '../classes/class.'.$class . '.php'; if ( file_exists($classpath)) { require_once $classpath; } //if call from deeper directory adjust the path $classpath = '../../classes/class.'.$class . '.php'; if ( file_exists($classpath)) { require_once $classpath; } } // 注册自定义自动加载函数 spl_autoload_register('my_autoloader');
补充说明:高版本PHP已经废弃了__autoload函数,改用spl_autoload_register是正确的方向,但必须注意不能用系统函数名来命名自己的函数,否则必然冲突报错。
内容的提问来源于stack exchange,提问作者SG-17
相关产品推荐
相关产品推荐

