Laravel-php's namespace and AutoLoad's problems

Source: Internet
Author: User
Keywords Php namespaces autoload thinkphp laravel
Tags autoload
Suppose one of the directories of my project:

app== site==== controller====== IndexController.php====== AccessController.php== api==== controller====== PostController.php

Suppose the content of indexcontroller.php:

namespace app\site\controller;class IndexController {    // some code}

The project is a single entry, containing the function autoload ($className), my question:

Assuming that the project contract has a name that is consistent with the folder name in the namespace, and that the class name is the same as the class file (without the odd characters such as underscores), then $className actually just need to replace the right slash with the Directory_separator You can find the corresponding class file, for example:

$obj = new \app\site\controller\IndexController();

Or

class TestClass extends \app\site\controller\IndexController {}

That AutoLoad actually found is/app/site/controller/indexcontroller.php.

In this case, why thinkphp inside the Think class to $_map it? Similar to the one in Laravel:

autoload_namespaces.phpautoload_classmap.phpautoload_psr4.php

For example, this is the content of autoload_psr4.php:

$vendorDir = dirname(dirname(__FILE__));$baseDir = dirname($vendorDir);return array(    'yii\\swiftmailer\\' => array($vendorDir . '/yiisoft/yii2-swiftmailer'),    'yii\\gii\\' => array($vendorDir . '/yiisoft/yii2-gii'),    'yii\\faker\\' => array($vendorDir . '/yiisoft/yii2-faker'),    'yii\\debug\\' => array($vendorDir . '/yiisoft/yii2-debug'),    'yii\\composer\\' => array($vendorDir . '/yiisoft/yii2-composer'),    'yii\\codeception\\' => array($vendorDir . '/yiisoft/yii2-codeception'),    'yii\\bootstrap\\' => array($vendorDir . '/yiisoft/yii2-bootstrap'),    'yii\\' => array($vendorDir . '/yiisoft/yii2'),    'cebe\\markdown\\' => array($vendorDir . '/cebe/markdown'),    'Faker\\' => array($vendorDir . '/fzaninotto/faker/src/Faker'),);

What does this have to do with the mapping defined by the array?

Reply content:

Suppose one of the directories of my project:

app== site==== controller====== IndexController.php====== AccessController.php== api==== controller====== PostController.php

Suppose the content of indexcontroller.php:

namespace app\site\controller;class IndexController {    // some code}

The project is a single entry, containing the function autoload ($className), my question:

Assuming that the project contract has a name that is consistent with the folder name in the namespace, and that the class name is the same as the class file (without the odd characters such as underscores), then $className actually just need to replace the right slash with the Directory_separator You can find the corresponding class file, for example:

$obj = new \app\site\controller\IndexController();

Or

class TestClass extends \app\site\controller\IndexController {}

That AutoLoad actually found is/app/site/controller/indexcontroller.php.

In this case, why thinkphp inside the Think class to $_map it? Similar to the one in Laravel:

autoload_namespaces.phpautoload_classmap.phpautoload_psr4.php

For example, this is the content of autoload_psr4.php:

$vendorDir = dirname(dirname(__FILE__));$baseDir = dirname($vendorDir);return array(    'yii\\swiftmailer\\' => array($vendorDir . '/yiisoft/yii2-swiftmailer'),    'yii\\gii\\' => array($vendorDir . '/yiisoft/yii2-gii'),    'yii\\faker\\' => array($vendorDir . '/yiisoft/yii2-faker'),    'yii\\debug\\' => array($vendorDir . '/yiisoft/yii2-debug'),    'yii\\composer\\' => array($vendorDir . '/yiisoft/yii2-composer'),    'yii\\codeception\\' => array($vendorDir . '/yiisoft/yii2-codeception'),    'yii\\bootstrap\\' => array($vendorDir . '/yiisoft/yii2-bootstrap'),    'yii\\' => array($vendorDir . '/yiisoft/yii2'),    'cebe\\markdown\\' => array($vendorDir . '/cebe/markdown'),    'Faker\\' => array($vendorDir . '/fzaninotto/faker/src/Faker'),);

What does this have to do with the mapping defined by the array?

Composer automatic loading can support four kinds of specifications, psr-0 (autoload_namespaces.php), Psr-4,class-map, and files, if you write your own code, so agreed, the namespace and path are consistent, Then autoload load by path loading, of course, do not need to classmaps, but a lot of class library is written by other people, everyone is accustomed to different, some people like psr-4, some people like psr-0, (everyone norms are the same, the Declaration of the namespace and path is not the same), you need to say Classmap, you can look at the composer Loader class.

Laravel inside the autoload_psr4.php is composer based on the Composer.json of each package automatically generated, you study the composer will understand

Automatic loading is more than what you see can directly

$obj = new \app\site\controller\IndexController();

These are the results of automatic loading.
If you do not load automatically, you cannot implement the object at all.

You get out of the frame and read it like that?

The automatic loading of composer is based on the PSR rules.

For a small example:
If you want to implement a factory class, how does auto-load implement?
The master can try and know why the framework is doing so.

Composer automatic loading can also be customized mapping, not necessarily by the normal directory automatically loaded, you can look at the composer document, architecture that chapter, there is a description of each key meaning.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.