Why not specify the file path after the namespace is introduced?

Source: Internet
Author: User
Tags autoload
Why not specify the file path after the namespace is introduced? Why not specify the file path after the namespace is introduced?

Reply content:

Why not specify the file path after the namespace is introduced?

Because there is a function used to automatically load files according to your namespace.

So it looks likeYou do not need to specify the file pathIn fact, there is an automatic loading mechanism to help you require.

Add:
LZ can go to the official documentation to see: http://php.net/manual/zh/function.autoload.php

Php's Automatic Loading Function registration is a simple example


  

Your proposition is wrong, so there is no need to explain why.

A namespace can be simply interpreted as a long class name. For example, a Project may have two objects: \ Core \ Object and \ Project \ Object (which belong to the Core and Project namespaces respectively .) During the call, use Core \ Object or use Project \ Object indicates whether the $ obj = new Object () in the subsequent code is a Core \ Object instance or a Project \ Object instance. This has nothing to do with the specified file path.

You do not need to specify the file path from PHP's automatic loading mechanism (including the default mechanism and custom extensions in the project), instead of using the namespace. A simple counterexample: ZendFramework1 does not use namespaces, but it also implements automatic loading.

You can create a new project with only two files.
Index. php
$ Object = new \ Object ();
Create a Project directory and file Object. php
Namespace \ Project
Class Object {}
Run index. php to check the results.

You can go to Baidu for researchspl_autoload_register

Unless the automatic loading mechanism is in operation, an error is reported. Let's take a look at the automatic loading mechanism.
Http://www.cnblogs.com/quinnxu/p/4824805...

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.