Home > Article > Backend Development > What should I do if the php use class cannot be found?
Solution for php use class not found: 1. Open the corresponding PHP file; 2. Insert the use statement into each file; 3. Execute "class_alias('RedBean_Facade', 'R');" Just code.
The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.
What should I do if the php use class cannot be found?
Problem Description:
About php: Class not found after requesting file containing use statement
I am trying to use a file to establish a database connection to RedBeanPHP, which can be included anywhere else. Example:
file1.php
<?php require_once '../vendor/autoload.php'; use RedBean_Facade as R; R::setup(); ?>
file2.php
<?php require_once 'file1.php'; R::debug(true); ?>
But navigating to file2.php shows this error:
Fatal error: Class 'R' not found in /file1.php on line 5
Name in PHP Won't the space use statement get include d?
Problem Solved:
In PHP, namespace and use statements are only valid in the physical file in which they appear. These statements do not cross require. They are already handled at compile time.
If a.php uses an alias in its namespace, and it is included in b.php, b.php will not see the alias defined in a.php.
You must insert use statements into each file. Here is the documentation:
Importing rules are per file basis
However, you can use class_alias to solve this problem:
class_alias('RedBean_Facade', 'R');
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What should I do if the php use class cannot be found?. For more information, please follow other related articles on the PHP Chinese website!