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


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");

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


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");

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
//Define two functions respectively
function model($name){
  $file = './model/'.$name.'.class.php';
    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';
    require './controll/'.$name.'.class.php';
//If you are registering a method instead of a function, you need to use an array
  //Non static method
  //Static method

Project scenario application

//Automatic loading
//Controller class model class core class
//All classes are divided into determinable classes and extensible classes
//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'
    require $framework_class_list[$name];
    require './application/'.PLATFORM.'/controller/'.$name.'.class.php';
    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!