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:
Will PHP not automatically load that class according to the name of the namespace?
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?
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:
Will PHP not automatically load that class according to the name of the namespace?
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?
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:
Use introduces only the space name, the flesh is not introduced;
PHP must be require and include to introduce PHP files;
Original aim, don't be confused by the illusion
Resources:
Autoload Self-loading
Composer Automatic load parsing
You can use the IDE tool to see the PHP related framework (such as Laravel) automatic parsing source
Certainly not automatically loaded unless you set it up spl_autoload_register()
.
Yes, or use a require_once
function such include_onece
as this to load the class file.
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:
-
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)
Framework programs, such as
-
thinkphp and Laravel, and composer, are implemented by using Spl_autoload_resigster
to load classes according to certain functions. The
-
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.
-
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.