Explanation of PHP Namespace and Automatic Loading Class
- 2021-11-01 02:27:34
- OfStack
This article illustrates the PHP namespace and auto-load classes. Share it for your reference, as follows:
Today, I'm going to introduce you to the namespace and auto-load classes of PHP
I'll simply separate the demonstration in the first place
Please look at:
What is an auto-load class?
I think everyone should know
__autoload
This magic method
$db = new DB();
function __autoload($className)
{
echo $className;
exit();
// Reference classes in this
}
In the use of this class can only be loaded once, in many cases we want to introduce more than 1 class at this time how to do!
The awesome php gods have come up with a new way to solve this problem. Please see this function
spl_autoload_register()
This kind of demand is met. It actually creates a queue of autoload functions, which are executed one by one in the order in which they were defined. In contrast,
__autoload()
Can only be defined once.
There are many uses of this function. Here, let's introduce one way to use it first
As follows:
function load1($className)
{
echo 1;
require $className . '.php';
}
spl_autoload_register('load1'); // Will load1 Function to the auto-load queue.
$db = new DB(); // Can't find DB Class, it will automatically call the newly registered load1 Function
//php 5.3 After that, you can also support anonymous functions like this.
spl_autoload_register(function($className){
if (is_file('./lib/' . $className . '.php')) {
require './lib/' . $className . '.php';
}
});
Multiple
spl_autoload_register
Use of
Determine if there is this file
If there is introduction, if there is no if, where does it come from if
function load1($className)
{
echo 1;
if (is_file($className . '.php')) {
require $className . '.php';
}
}
function load2($className)
{
echo 2;
if (is_file('./app/' . $className . '.php')) {
require './app/' . $className . '.php';
}
}
function __autoload($className)
{
echo 3;
if (is_file('./lib/' . $className . '.php')) {
require './lib/' . $className . '.php';
}
}
// Registered 3 A
spl_autoload_register('load1');
spl_autoload_register('load2');
spl_autoload_register('__autoload');
$db = new DB(); //DB It's in this directory
$info = new Info(); //Info In /lib/Info.php
We can print
spl_autoload_functions()
Function to show how many auto-loads are registered in 1:
var_dump(spl_autoload_functions());
// Formal output of array
array (size=3)
0 => string 'load1' (length=5)
1 => string 'load2' (length=5)
2 => string '__autoload' (length=10)
Namespace
Is not supported in 1 php file
new
Two-name
At this time, we can use the namespace
namespace app;
class new{
public function ten()
{
echo "85";
}
}
namespace hii;
class new{
public function ten()
{
echo "85";
}
}
spl_autoload_register
Auto Load + Use of namespace Namespace
Don't talk nonsense. Look at examples
AutoLoading\loading
<?php
namespace AutoLoading;
class loading {
public static function autoload($className)
{
// Put \ Transfer floor / , Easy compatibility Linux File search. Windows The following is universal
// Due to namspace It's very standard, so it can be found directly and quickly
$fileName = str_replace('\\', '/', DIR . '\\'. $className) . '.php';
if (is_file($fileName)) {
require $fileName;
} else {
echo $fileName . ' is not exist'; die;
}
}
}
The above is a core idea of automatic loading. We'll come next
spl_autoload_register
To register this function:
index.php
<?php
// Defines the current directory absolute path
define('DIR', dirname(__FILE__));
// Load this file
require DIR . '/loading.php';
// Adopt ` Namespace ` Register in the way of. php 5.3 Joined
// It has to be, too. It has to be static Static method call, and then just like loading namespace Called in the way of, note: You cannot use use
spl_autoload_register("\\AutoLoading\\loading::autoload");
// Call 3 A namespace Class
// Navigate to Lib Directory Name.php
Lib\Name::test();
// Navigate to App Directory Android Directory Name.php
App\Android\Name::test();
// Navigate to App Directory Ios Directory Name.php
App\Ios\Name::test();
Because we use PSR-O to define
namespace
It is named, so it is good to locate which directory this file is in. It's cool. Right?
APP\Android\Name
namespace App\Android;
class Name
{
public function __construct()
{
echo __NAMESPACE__ . "<br>";
}
public static function test()
{
echo __NAMESPACE__ . ' static function test <br>';
}
}
So it will be easy to find the file and output:
Lib static function test
App\Android static function test
App\Ios static function test
All right. That's the basics.
For more readers interested in PHP related content, please check the topics on this site: "Introduction to php Object-Oriented Programming", "Introduction to PHP Basic Syntax", "Summary of PHP Operation and Operator Usage", "Summary of PHP Network Programming Skills", "Encyclopedia of PHP Array (Array) Operation Skills", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation Skills" and "Summary of php Common Database Operation Skills"
I hope this paper is helpful to everyone's PHP programming.