Get the App
SLTechnology News&Howtos  ›  Development  › 

How to solve the problem of automatic loading failure of php class

Shulou Source: shulou.com Published: 2022-06-03 10:44:34 10月20日 Update

This article shows you how to solve the problem of php class auto-loading failure, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

This article operating environment: windows7 system, PHP7.1 version, Dell G3 computer.

How to solve the problem of auto-loading failure of php class?

Automatic loading under the PHP namespace failed

Scene description

The file is tested without exception under the local win system, 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 occurred when deployed to a Ubuntu server and an error was reported as unable to autoload Class xxxxxx

Solution

According to the error report, it is found that the value of $class needs to be shaped like stuApp\ dao\ StuInfo to be feasible, and the file path needs to escape\ into /, so add a line of code.

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

To sum up, the modified auto-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") The above content is how to solve the problem of automatic loading failure of php class. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Tags: Questions code content skills files knowledge systems paths escape feasible concise concise one line scenario that is articles solutions more servers environments Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL macOS Shulou Technology Docker Redmi