多年前,我为我的网站使用了一个简单的php博客平台,这使用了旧的__autoload函数.我最近不得不迁移主机,他们使用了一个新版本的php,所以这个功能不再起作用.我try 用spl_autoload_register替换_autoload,但标题中出现错误.我不熟练的php和我不能弄清楚如何解决这个问题自己.下面是我的php文件中的代码.

//load classes as needed
function spl_autoload_register($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 within admin adjust the path
   $classpath = '../../classes/class.'.$class . '.php';
   if ( file_exists($classpath)) {
      require_once $classpath;
    }       
     
}

推荐答案

spl_autoload_register()是一个现有的PHP函数,不能重新定义.试着这样使用它:

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 within admin adjust the path
   $classpath = '../../classes/class.'.$class . '.php';
   if ( file_exists($classpath)) {
      require_once $classpath;
    }       
     
}

spl_autoload_register('my_autoloader');

我省略了代码的其余部分,因为这并不是问题的真正内容.

有关更多信息,请参阅:spl_autoload_register()

Php相关问答推荐

在PHP中替换数组中的文本并同时使用其他函数

如何使用URL链接(mdl)切换选项卡

照明\Support\Collection::Filter():参数#1($Callback)的类型必须为?可调用、已给定数组、已调用

使用PHP和WebSockets或SSE实现实时通知

Symfony Api平台:按自定义实体方式排序结果

RediSearch PHP希望我在每次使用时定义所有字段

在WooCommerce中添加基于类别的固定费用

多行PHP属性声明中是否允许单行注释

将日期数组排序为年->;月

将SVG包含在另一个php生成的SVG中

我的邮箱中的链接在我的URL中添加了一个

对表示大小的值后缀进行自定义排序(XXS、XS、S、M、L、XL、XXL)

PHP 中使用 JSON 的自定义会话处理程序会因错误而 destruct 会话

图像未在 Laravel Ajax Crud 中显示

自定义主键值未正确显示

同时执行odbc_execute、odbc_fetch_row和odbc_result

使用循环和引用而不是递归创建分层/父子数组

在 EasyAdmin 中添加全局操作,将我的按钮放在表格下方而不是上方

根据数组中的文本文件编号显示星级

标头内容类型:image/jpeg 无效