Home  >  Article  >  Backend Development  >  What should I do if the php use class cannot be found?

What should I do if the php use class cannot be found?

藏色散人
藏色散人Original
2022-10-20 10:28:452072browse

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.

What should I do if the php use class cannot be found?

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 &#39;../vendor/autoload.php&#39;;
use RedBean_Facade as R;
R::setup();
?>

file2.php

<?php
require_once &#39;file1.php&#39;;
R::debug(true);
?>

But navigating to file2.php shows this error:

Fatal error: Class &#39;R&#39; 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(&#39;RedBean_Facade&#39;, &#39;R&#39;);

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:How to hide name in phpNext article:How to hide name in php