Loading problems with PHP namespaces

Source: Internet
Author: User
Tags autoload zend framework

Let's assume that the directory structure is now:

The code inside the a.php is:

namespace A;
Class a{
Public Function __construct ()
{
Echo ' AAAAAAAAAAAA ';
}
}

The code inside the b.php is:
namespace B;
Use a\a;
New A ();
?>

Error: Fatal error:class ' AA ' not found in ...

I have always wondered about the namespace naming rules for PHP:

    1. Will PHP not automatically load that class according to the name of the namespace?

    2. Is it possible to use require, include, and so on to load another class file into the current file to instantiate when a file calls another file's class, even if the namespace is used?

    3. If the 2nd yes above, is not to use autoload and other ways to load? is not the so-called namespace is actually a name used to distinguish the class, and does not automatically load the role of the class?

Reply content:

Let's assume that the directory structure is now:

The code inside the a.php is:

namespace A;
Class a{
Public Function __construct ()
{
Echo ' AAAAAAAAAAAA ';
}
}

The code inside the b.php is:
namespace B;
Use a\a;
New A ();
?>

Error: Fatal error:class ' AA ' not found in ...

I have always wondered about the namespace naming rules for PHP:

    1. Will PHP not automatically load that class according to the name of the namespace?

    2. Is it possible to use require, include, and so on to load another class file into the current file to instantiate when a file calls another file's class, even if the namespace is used?

    3. If the 2nd yes above, is not to use autoload and other ways to load? is not the so-called namespace is actually a name used to distinguish the class, and does not automatically load the role of the class?

The first thing to know is what the namespace is, and the namespace, as its name, is the name of the space that declares itself (Classes\tool: the equivalent of announcing-I'm in the tool space in the classes space), in other words, where the surface is, the namespace you introduced with use, In the final analysis just introduced a "location name", the flesh is not introduced by the include or require, the introduction of PHP into the flesh must be through the include or require, out of the two is impossible.

We see __autoload, Spl_autoload_register belongs to the magic introduction of PHP (in fact, this concept is similar to object-oriented control inversion (personal understanding)), Magic Introduction-The popular point is to produce a magic box, This magic box is responsible for dealing with the cumbersome include and require these dirty. If you want to implement the automatic loading through the namespace, you need to follow the rules, the rule is: psr-0 automatic loading specification, if the rule declares the location name, by using the "location name" introduced by the Magic Box can be parsed, parse its location name, retrace, The corresponding file is then imported through include or require.

Sum up on these three points:

    1. Use introduces only the space name, the flesh is not introduced;

    2. PHP must be require and include to introduce PHP files;

    3. Original aim, don't be confused by the illusion

Resources:

    1. Autoload Self-loading

    2. Composer Automatic load parsing

    3. You can use the IDE tool to see the PHP related framework (such as Laravel) automatic parsing source

    1. Certainly not automatically loaded unless you set it up spl_autoload_register() .

    2. Yes, or use a require_once function such include_onece as this to load the class file.

    3. Namespaces can be considered directories, and different directories can have the same file name, avoiding naming conflicts. Namespaces do not have the function of automatically loading classes.

1. does not load automatically

自动加载:    类库映射    PSR-4自动加载检测    PSR-0自动加载检测

2. In TP5, you can use the keyword, do not need to do require such cumbersome operation
3. Spl_autoload_register has been used to replace the autoload, composer is to solve the problem of loading? So be sure to set the namespace, the namespace does not have the role of the auto-load class, He is designed to avoid naming conflicts and visualizing the path of the class and really lazy loading and so on.

Use Spl_autoload_register to register an auto-load function to use Use,demo:


  
   

  1. Pho does not automatically load classes. spl_autoload is loaded from the include configuration to find the corresponding class name. Inc or class name. PHP to load. (See PHP documentation for details)

  2. Framework programs, such as
  3. thinkphp and Laravel, and composer, are implemented by using Spl_autoload_resigster to load classes according to certain functions. The

  4. Namespace setting is intended to differentiate between classes of the same name in different extents, not necessarily by folder names, but by partitioning the class, for example, you have a class called Router I have a class called Router , the class name is duplicated, and the namespace can work.

  5. My answer is that thinkphp's principle is to parse the path through a namespace, and then register the autoload to load, such as ABC, to find a/bc.class.php in the specified directory. class.php is the load suffix specified by TP, the specified directory is usually the library, and the Zend framework uses _ to divide the path, such as the A_b_c class. will become/a/b/c.php to find in the specified directory.

To learn about PHP object-oriented design mode they're talking about this.

  • 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.