|
FAQ: things you need to know about namespacesThis FAQ is split into two sections: common questions, and some specifics of implementation that are helpful to understand fully. First, the common questions.
There are a few implementation details of the namespace implementations that are helpful to understand.
If I don't use namespaces, should I care about any of this?No. Namespaces do not affect any existing code in any way, or any as-yet-to-be-written code that does not contain namespaces. You can write this code if you wish:
Example #1 Accessing global classes outside a namespace
<?php This is functionally equivalent to:
Example #2 Accessing global classes outside a namespace
<?php How do I use internal or global classes in a namespace?
Example #3 Accessing internal classes in namespaces
<?php How do I use namespaces classes, functions, or constants in their own namespace?
Example #4 Accessing internal classes, functions or constants in namespaces
<?php How does a name like \my\name or \name resolve?Names that begin with a \ always resolve to what they look like, so \my\name is in fact my\name, and \Exception is Exception. Example #5 Fully Qualified names
<?php How does a name like my\name resolve?Names that contain a backslash but do not begin with a backslash like my\name can be resolved in 2 different ways. If there is an import statement that aliases another name to my, then the import alias is applied to the my in my\name. Otherwise, the current namespace name is prepended to my\name.
Example #6 Qualified names
<?php How does an unqualified class name like name resolve?Class names that do not contain a backslash like name can be resolved in 2 different ways. If there is an import statement that aliases another name to name, then the import alias is applied. Otherwise, the current namespace name is prepended to name.
Example #7 Unqualified class names
<?php How does an unqualified function name or unqualified constant name like name resolve?Function or constant names that do not contain a backslash like name can be resolved in 2 different ways. First, the current namespace name is prepended to name. Finally, if the constant or function name does not exist in the current namespace, a global constant or function name is used if it exists.
Example #8 Unqualified function or constant names
<?php Import names cannot conflict with classes defined in the same file.The following script combinations are legal: file1.php
<?php another.php
<?php file2.php
<?php There is no name conflict, even though the class MyClass exists within the my\stuff namespace, because the MyClass definition is in a separate file. However, the next example causes a fatal error on name conflict because MyClass is defined in the same file as the use statement.
<?php Nested namespaces are not allowed.PHP does not allow nesting namespaces
<?php
<?php Before PHP 5.6 neither functions nor constants can be imported via the use statement.Before PHP 5.6 the only elements that are affected by use statements are namespaces and class names. In order to shorten a long constant or function, import its containing namespace.
<?php Dynamic namespace names (quoted identifiers) should escape backslashIt is very important to realize that because the backslash is used as an escape character within strings, it should always be doubled when used inside a string. Otherwise there is a risk of unintended consequences: Example #9 Dangers of using namespaced names inside a double-quoted string
<?php Undefined Constants referenced using any backslash die with fatal errorAny undefined constant that is unqualified like FOO will produce a notice explaining that PHP assumed FOO was the value of the constant. Any constant, qualified or fully qualified, that contains a backslash will produce a fatal error if not found. Example #10 Undefined constants
<?php Cannot override special constants NULL, TRUE, FALSE, ZEND_THREAD_SAFE or ZEND_DEBUG_BUILDAny attempt to define a namespaced constant that is a special, built-in constant results in a fatal error Example #11 Undefined constants
<?php |