In the previous article, I brought you "How to determine whether an object belongs to a class in PHP? ", which details the relevant knowledge that should be used to determine whether an object belongs to a class in PHP. In this article, we will take a look at the automatic loading mechanism in PHP. I hope everyone has to help!
In daily development and use, the basic idea of our object-oriented programming is that it is usually customary to create a separate PHP source file for each class. In this case It provides convenience for subsequent maintenance and makes it easy to reuse classes.
In PHP, the automatic loading function of the class can be realized through the spl_autoload_register()
and __autoload()
functions, which can save our programming time and energy. Then let’s introduce these two functions separately.
<span style="font-size: 20px;"><strong><span style="font-size: 18px;">__autoload()</span></strong></span>
##Function
__autoload() function is a magic method to be precise. We will take you to understand the magic method in PHP in five minutes (detailed examples)
》Introduces some commonly used magic methods in detail. It is mentioned that it is automatically called, that is, the function needs to be called under specific conditions. When we create a new class, if the class cannot be found in the current source file, PHP will automatically call the
function and pass the class name to __autoload() function. This is the specific condition for the __autoload() function to be called. Its syntax format is as follows: <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">function __autoload($class){
// 方法体
}</pre><div class="contentsignin">Copy after login</div></div>
What we need to pay attention to is:
is the name of the class to be loaded.
The function can only be defined once in the current source file.
function to automatically load a class file, the name of the class file needs to be the same as the class name. Only one class can be defined in another class file .
<?php function __autoload($class){ $file = './'.$class.'.php'; include_once($file); } $obj = new Demo(); ?>
Run the above code, it will automatically load the files in the same directory. Demo.php file, the code in Demo.php is as follows:
<?php class Demo{ } ?>
What we need to pay attention to is: __autoload() function has been deprecated since PHP7.2.0, you can use spl_autoload_register() function instead .
<span style="font-size: 20px;">spl_autoload_register()<strong><span style="font-size: 18px;"></span></strong></span>
## Functionspl_autoload_register() function can specify a function to replace the function of __autoload() function,
spl_autoload_register([$autoload_function [, $throw = true [, $prepend = false ]]])
What needs to be noted is:
<?php spl_autoload_register('loadClass'); function loadClass($class){ $file = './'.$class.'.php'; include_once($file); } $obj = new Demo(); ?>
PHP Video Tutorial
" to learn more about PHP knowledge.The above is the detailed content of How to use spl_autoload_register() and __autoload() functions in PHP?. For more information, please follow other related articles on the PHP Chinese website!