PHP command namespace and use of space usage

The purpose of using the namespace:

When the project team, other team members to avoid new class conflict; personal charge of the project, before and after class to avoid new conflicts;

According to understand, when used in the required categories, we need to require or include the introduction, so false premise occur redefined class are: two identical named class has been introduced. At present, some php framework will automatically load (ie, include) all new model class, so in order to avoid core classes like your new model and framework of the project the same name conflict occurs native, uses a namespace. (Thought, and new team members communicate through class conflict should be avoided, even if the incident should re-adjust to maintain real-time class names to avoid confusion because the latter caused the class to understand the complexity brought on improving maintenance)

 

namespace to use:

Named after 1.namespace definition is not case sensitive

2, a PHP file room multiple namespaces, can not have the code before the first namespace; namespace suggested class name

3. namespace is not defined, it is understood as the use of top-level namespace. When the new class, you can put a backslash in front of the class \, you can not add.

4, in the current namespace, the default will add its name; "\" add on, except at the top

5, the class specified namespace, when the new class must be specified to bring Bitter space; did not bring the specified namespace, php will find this class from the top level namespace .

Remember: There can not contain all of the namespace in accordance with other top-level namespace to understand. The top-level namespace should be completely separate from other namespaces area.

Examples are as follows:

Anlis.php

? < PHP 
namespace One; 
class Animal {
     function __construct () 
    { 
        echo "! My namespace called One <br>" ; 
    } 
} 

new new Animal ();
 // new new \ Animal (); // C: \ phpStudy \ PHPTutorial \ WWW \ test \ yidian \ namespace \ anli .php not found "Animal" category, because the top-level namespace and other namespaces are separate
the Test namespace; 
class the Person {
function __construct ()
{
echo "My namespace called <br> test2!";
}
}

TWO namespace; 
class the Person { function __construct () { echo "My namespace called <br> the Person!" ; } }

// When the new class, when put on namespace between certain characters with a backslash; earlier also to add "\", if not the foremost plus the current default namespace "two", being given new new \ One \ Animal (); // new new \ Animal (); // error code is: C: \ phpStudy \ PHPTutorial \ WWW \ test \ yidian \ namespace \ anli .php is not found in Class 'Animal' new new \ the Test \ the Person (); // specified namespace, the output of the "my namespace test2!" new new the Person (); // default coupled with the current two

 

 

Purpose of use:

When namespace string is too long, the use of use can be correspondingly shortened namespace.

 

use of use:

1, when the new type, without the front with a backslash. In addition, there is no use as the shortening of the default namespace for the content after the last backslash.

2, use can have an alias, you can also directly use the class name

Note: In the current namespace, use the same name can not be, must be used as, otherwise an error

use One \ Animal; // default Animal class name
 new new Animal ();
 use the Test \ the Person AS b; // using the alias
 new new b;

 to sum up:

1, namespace is the role of divided areas, representatives of these things belong under a namespace.

2, use is to act as a nickname, whether to write or say it can save a lot of thing.

 

Guess you like

Origin www.cnblogs.com/bushui/p/11486689.html