Home  >  Article  >  Backend Development  >  Class automatic loading instance analysis in PHP

Class automatic loading instance analysis in PHP

小云云
小云云Original
2018-03-31 09:57:561264browse

When you need to use php code in another file in php, you can use include, include_once, require, and require_once to introduce class files. The difference between require and include is the difference in syntax strictness: when an error occurs in the imported file, an error will be reported using the include syntax, but the subsequent statements will continue to execute. require will stop execution after an error occurs.

The difference between include and include_once is that when include is used, it will be introduced once when it exists, while include_once will only be introduced once when the same file is introduced.

Generally, since a large number of imported files may be included in use, a large number of include/require statements need to be written. This is extremely inconvenient. For this situation, you can use automatic loading.

1. Use the __autoload() magic function:

When the program "requires a class", the system will automatically call this function, which needs to be defined by ourselves:

function __autoload($className){
	require "需要引入文件的路径(不包含文件名)".$className.".php";
}

The variable $className is the class name passed in when the function is automatically called.

2. Use the spl_autoload_register() function:

Use it to declare multiple functions that can be used instead of __autoload(),

spl_autoload_register(" Function name 1");

spl_autoload_register("Function name 2");...... This function is actually the same as __autoload();

//此处声明三个自动加载函数名,
spl_autoload_register('auto1');
spl_autoload_register('auto2');
spl_autoload_register('auto2');
//接下来需要定义所声明的函数。
function auto1($className){
	$file="文件路径".$className.".php";
	if(file_exists($file)){
		require $file;
	}
}
function auto2($className){
	$file="文件路径".$className.".php";
	if(file_exists($file)){
		require $file;
	}
}
function auto2($className){
	$file="文件路径".$className.".php";
	if(file_exists($file)){
		require $file;
	}
}
在使用该方法时,需要类时,会按照声明的顺序先从auto1()调用,如果有的话就引入,如果不存在,就到下一个函数调用......

The difference between spl_autoload_register() and __autoload() is that spl_autoload_register() can introduce different files from different file paths. And when spl_autoload_register() is registered in the file, the method __autoload() is automatically invalid.

Related recommendations:

PHP implements automatic loading function

PHP implements routing and class automatic loading

PHP implements automatic loading of related functions

The above is the detailed content of Class automatic loading instance analysis in PHP. 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