Processing scheme and example code for automatic loading failure of PHP class

Time:2022-1-13

1. Open the corresponding PHP code file.

2. Add “$class = str_replace (” \ \ “,” / “, $class);” Just code.

The file is tested in the local win system without exception, and the code is as follows:


function stu_autoload($class){
    if(file_exists($class.".php")){        require ( $class.".php");
    }else{        die("unable to autoload Class $class");
    }
}
spl_autoload_register("stu_autoload");

An exception occurs when deploying to an Ubuntu server. The error is unable to autoload class XXXXXX

Solution

According to the error report, it is found that the value of $class needs to be in the form of stuapp \ Dao \ stuInfo, and the file path needs to escape \ into /, so add a line of code.


$class = str_replace("\\","/",$class);

To sum up, the modified automatic loading code is as follows:

function stu_autoload($class){
    //Path escape
    $class = str_replace("\\","/",$class);    if(file_exists($class.".php")){        require ( $class.".php");
    }else{        die("unable to autoload Class $class");
    }
}
spl_autoload_register("stu_autoload");

Knowledge point expansion:

Automatic loading of classes

In the external page, there is no need to introduce class files, but the program will automatically “dynamically load” a class when it is needed.

① New when creating an object

② Use a class name directly (manipulate static properties and methods)

Using SPL_ autoload_ register()

Use it to register (declare) multiple alternatives__ Autoload() functions naturally have to define these functions, and the functions are the same as__ Autoload () works the same, but it can handle more situations at this time

//Register functions for automatic loading
spl_autoload_register("model");
spl_autoload_register("controll");
//Define two functions respectively
function model($name){
  $file = './model/'.$name.'.class.php';
  if(file_exists($file)){
    require './model/'.$name.'.class.php';
  }
}
//If you need a class, but the class has not been loaded on the current page
//Model () and control () will be called in turn until such a file is found to load, otherwise an error will be reported
function controll($name){
  $file = './controll/'.$name.'.class.php';
  if(file_exists($file)){
    require './controll/'.$name.'.class.php';
  }
}
//If you are registering a method instead of a function, you need to use an array
spl_autoload_register(
  //Non static method
  array($this,'model'),
  //Static method
  array(__CLASS__,'controller')
);

Project scenario application

//Automatic loading
//Controller class model class core class
//All classes are divided into determinable classes and extensible classes
spl_autoload_register('autoLoad');
//First deal with the determined framework core classes
function autoLoad($name){
  //Class name and class file mapping array
  $framework_class_list = array(
    'mySqldb' => './framework/mySqldb.class.php'
  );
  if(isset($framework_class_list[$name])){
    require $framework_class_list[$name];
  }elseif(substr($name,-10)=='Controller'){
    require './application/'.PLATFORM.'/controller/'.$name.'.class.php';
  }elseif(substr($name,-6)=='Modele'){
    require './application/'.PLATFORM.'/modele/'.$name.'.class.php';
  }
}

This is the end of this article on the handling scheme and example code of PHP class automatic loading failure. For more solutions to PHP class automatic loading failure, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!