Detailed explanation of composer automatic loading mechanism
藏色散人
Release: 2020-07-06 13:35:39
forward
3556 people have browsed it
The following tutorial column of composer will introduce the composer automatic loading mechanism from shallow to deep. I hope it will be helpful to friends in need!
PrefaceBecause for composer automatically The only memory left in the loading mechanism is "spl_auto???" and "deriving file paths based on namespace". . . Still incomplete. . I wanted to collect a detailed explanation online, but I couldn’t find an article that fits my opinion of "from the simplest to the deeper".
So I have this note.
The following knowledge points are coming soon:
1. Learn about spl_autoload_register 2. The story of composer update 3. Track the automatic loading of composer
Text1. Learn about spl_autoload_registerFirst check the official php manual:
(You can just look at the red part if you are lazy)
Is that right? Seemingly ignorant?
Let’s translate it in vernacular:
If we create a new class, we must first require or include the class file. If it is not loaded, an error will be reported. This creates a problem: in this case, the header of the file is full of requirements and includes, which is obviously not in line with the programmer's need to be "lazy".
In order to create a new class normally without requiring or including a class file, an automatic loading mechanism has emerged. The spl_autoload_register function is specifically designed to do this.
From the screenshot, we know that this function has three parameters:
Parameters
Detailed explanation
autoload_function
Fill in here is the name of a ***"function"***, a string or an array. The function of this function is to load the file that needs new Require or include as much as possible to avoid reporting errors when using new. To put it simply, you need to encapsulate a ***function that automatically loads files***
throw
When the automatically loaded function cannot be registered, whether to throw Exception
prepend
Whether to add the function to the head of the function queue, if true, it is the head, otherwise the tail
来一波代码,印象深刻一些:
//文件 testClass.php ,即将new的类
class TestClass{
public function __construct() {
echo '你已经成功new了我了';
}
}
//文件autoloadDemo.php文件
spl_autoload_register('autoLoad_function', true, true);
function autoLoad_function($class_name){
echo "所有的require或者include文件工作都交给我吧!\r\n";
$class_filename = "./{$class_name}.php";
echo "我来加载{$class_filename}文件\r\n";
require_once("./{$class_name}.php");
}
$obj_demo = new TestClass();
public function loadClass($class) {
if ($file = $this->findFile($class)) {
includeFile($file);
return true;
}
}
Copy after login
大概可以看出,是做了文件的include。 继续跟踪下是怎么查找文件的,看findFile函数:
public function findFile($class) {
// class map lookup
if (isset($this->classMap[$class])) {
return $this->classMap[$class];
}
if ($this->classMapAuthoritative || isset($this->missingClasses[$class])) {
return false;
}
if (null !== $this->apcuPrefix) {
$file = apcu_fetch($this->apcuPrefix.$class, $hit);
if ($hit) {
return $file;
}
}
$file = $this->findFileWithExtension($class, '.php');
// Search for Hack files if we are running on HHVM
if (false === $file && defined('HHVM_VERSION')) {
$file = $this->findFileWithExtension($class, '.hh');
}
if (null !== $this->apcuPrefix) {
apcu_add($this->apcuPrefix.$class, $file);
}
if (false === $file) {
// Remember that this class does not exist.
$this->missingClasses[$class] = true;
}
return $file;
}
The above is the detailed content of Detailed explanation of composer automatic loading mechanism. For more information, please follow other related articles on the PHP Chinese website!
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