Home  >  Article  >  Backend Development  >  Detailed explanation of usage of autoload method implementation in Laravel

Detailed explanation of usage of autoload method implementation in Laravel

巴扎黑
巴扎黑Original
2017-08-23 13:45:381911browse

This article will explain to you how to implement autoload in laravel? After analysis, I found that it is really clever. Let me give you a detailed explanation below.

After using Laravel for a while, I found that there are very few include and require. I thought it was a bit strange. I thought about how Laravel completes file import. .

In fact, Laravel still uses include or require, but they are all written in a function.

Like this:


function autoload($path){
	require_once($path);
}

Generally, there are more than one or two PHP files imported each time, there will be many, so an array must be defined to store " "Mapping relationship between classes and PHP files", like this:


$classmap=[
		'DB'=>'api.php',
		'FileSystem'=>'api.php',
		'Session'=>'api.php'
];

Then, traverse the array, don't write a for loop anymore, just use an elegant function solve.


array_map("autoload",$classmap);

Let’s take a look at how Laravel actually defines this array. The mapping file is in autoload_classmap.php of \vendor\composer

The above is the detailed content of Detailed explanation of usage of autoload method implementation in Laravel. 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