Home >Backend Development >PHP Tutorial >laravel-php Many frameworks import related classes directly through namespaces, but namespaces don't actually have that function? Explain it
Many PHP frameworks import related classes directly through namespaces, but namespaces don’t actually have that function? Please explain
Import a class using require_once include_once()...
But many frameworks, such as thinkphp, laravel, etc., do not use these methods, but use namespaces
What’s going on?
Many PHP frameworks import related classes directly through namespaces, but namespaces don’t actually have that function? Please explain
Import a class using require_once include_once()...
But many frameworks, such as thinkphp, laravel, etc., do not use these methods, but use namespaces
What’s going on?
The other two answers mentioned some points, but they are not complete enough
First of all, this is the feature of automatic loading
Auto-loading was introduced in PHP5. You can use spl_autoload_register
to register an auto-loading function. When PHP is looking for a class that does not exist in memory, it will call the auto-loading function registered in spl_autoload_register
in turn. Find class
Another way that is also available but not good is to override the __autoload
function, but then you can only have one autoloading function. If you also use some other libraries, it may cause some problems, so there is no need to do it. This is not recommended
As for composer, it comes with a standard autoload implementation. If you can make your code follow the directory organization of PSR, you can also directly use the autoload implementation provided by composer. You can simply learn about PSR
It should be noted that the auto-loading feature has nothing to do with namespaces. Although it is recommended to use it with namespaces, you will find in some old PHP codes that it is actually implemented using underscores _
. The effect of namespace
Yes, but it is not written directly in the logic code. For your question, you can try searching for php automatic loading and composer automatic loading
You can check the usage of the namespace use. Use introduces classes, so there is no need for require_once include_once()...
In fact, use does not introduce the function, but it is introduced automatically when the program automatically loads the __autoload method. Related classes