致命错误:无法使用简单的 php 博客重新声明 spl_autoload_register()

问题描述 投票:0回答:1

很多年前我的网站使用了一个简单的 php 博客平台,这使用了旧的 __autoload 函数。我最近不得不迁移主机,他们使用较新版本的 php,因此该功能不再起作用。我尝试用 spl_autoload_register 替换 __autoload 但我在标题中收到错误。我不擅长 php,我自己不知道如何解决这个问题。下面是我的 config.php 文件中的代码。

<?php
ob_start();
session_start();

//database credentials
define('DBHOST','localhost');
define('DBUSER','XXXX');
define('DBPASS','XXXX');
define('DBNAME','XXXX');

$db = new PDO("mysql:host=".DBHOST.";port=XXXX;dbname=".DBNAME, DBUSER, DBPASS);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


//set timezone
date_default_timezone_set('America/New York');

//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;
    }       
     
}

$user = new User($db); 

include('functions.php');
?>
php autoload spl-autoload-register
1个回答
0
投票

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()

© www.soinside.com 2019 - 2024. All rights reserved.