Studyon Minte9.com
ZCE 5.3

Study

Namespaces



	// prevent name collisions between different packages
	// namespace convention related to folder structure (helps autoloading)
	// namespace applies to the entire file: namespace ns ...
	// better with braches: namespace ns { ... }
	// global namespace: namespace { ... }
	// any code allowed within namespace but
	// only class, interfaces, functions & constants are affected 
	// fully, qualified, unqualified names
	// use ... as (alias) Path1/Path2; set E; alias as E/Path3
	// use one namespace per file (best practice)

PHP 5 >= 5.3.0, PHP 7

* Atention: | means BACKSLASH
	(we have replaced backshash with | for database storage reasons)

* Namespaces

PHP namespaces have been designed to prevent name collisions between classes from different packages 
and to avoid the use of very long names in the code to refer to classes or functions - nobody really wants to 
have to deal with something called Zend_Db_Adapter_Mysqli or 
PHPUnit_Framework_Constraint_IsInstanceOf, after all.

Without namespace (you can't have conflicts between different libraries).

<?php // test.php require("com/yahoo/MyClass.php"); echo MyClass::A; // Output 1 require("com/google/MyClass.php"); echo MyClass::A; // --- Fatal error: Cannot redeclare class MyClass --- // <?php // com/yahoo/MyClass.php class MyClass { const A = 1; } <?php // com/google/MyClass.php class MyClass { const A = 2; }
  Using namespaces (it will avoids conflicts)
<?php require("com/yahoo/MyClass.php"); require("com/google/MyClass.php"); echo com | yahoo | MyClass::A; // --- Output 1 --- // echo com | google | MyClass::A; // --- Output 2 --- // <?php // com/yahoo/MyClass.php namespace com|yahoo; class MyClass { const A = 1; } <?php // com/google/MyClass.php namespace com | google; class MyClass { const A = 2; }
  It's easy with autoload (no require file needed)
<?php function classAutoLoader($class) { $tmp = explode('', $class); require join("/", $tmp) . '.php'; } spl_autoload_register("classAutoLoader"); // --- Look Here --- // echo com | yahoo | MyClass::A; // Output 1 echo com | google | MyClass::A; // Output 2 <?php // com/yahoo/MyClass.php namespace com | yahoo; class MyClass { const A = 1; } <?php // com/google/MyClass.php namespace com | google; class MyClass { const A = 2; }
  * Defining Namespaces A namespace is defined with the namespace keyword and a name. It must be declared before any other code. Once defined, the scope of the namespace applies to the entire file.
namespace MyNamespace;
A namespace may alternatively be defined by wrapping brackets around its contents. No code is allowed outside the brackets. These two methods cannot be mixed, so choose one and stick to it.
namespace MyNamespace { // ... }
Nested namespaces can be defined by separating each level with a backslash.
namespace MyProject|Blog|Admin;
Multiple namespaces may be defined in a single file by using the namespace keyword multiple times. In such instances, the scope of the preceding namespace ends once the next namespace definition appears.
namespace MyProjectBlog; // ... namespace MyProjectStore; // ...
To set part of the file's code in the global namespace, use the namespace keyword without a name. This feature can only be used in conjunction with the curly bracket syntax.
namespace MyProject { // MyProject namespace // ... } namespace { // Global namespace // ... }
* Referencing Namespaced Identifiers Although any code can appear within the namespace, only classes, interfaces, functions and constants are affected by it. These identifiers can be referenced in one of three ways: *** Fully-qualified name
// A fully-qualified name is like an absolute path in a file system $a = new | myRoot | myLibrary | classes | myClass;
*** Qualified name
// A qualified name is like a relative path in a file system $a = new myRoot | myLibrary | classes | myClass;
*** Unqualified name
// It refers to the current namespace only $a = new myClass; // If is not found in the current scope // It will just resolve the identifier to the global namespace
Notice that to reference classes and interfaces in the global namespace, you must begin their names with a backslash (use their fully-qualified name). The functions do not need backslash.
<?php namespace myLibrary | classes; $obj = new ArrayObject(); // Fatal error: Class not found $obj = new | ArrayObject(); // Corect echo strlen("Hello World"); // Corect // Resolves to native function
  * Alias In a large application that contains many, deeply-nested namespaces, it can become cumbersome to write out identifier names. To avoid this, you can import namespaces, classes and interfaces using the use keyword. This allows them to be referenced by an alias instead of their full name. PHP namespaces support three kinds of aliasing or importing: aliasing a class name, aliasing an interface name, and aliasing a namespace name. Note that importing a function or constant is not supported.
<?php function classAutoLoader($class) { $tmp = explode('', $class); require join("/", $tmp) . '.php'; } spl_autoload_register("classAutoLoader"); use com | yahoo as y; // --- Look Here --- // use com | google as g; echo y | MyClass::A; // Output 1 echo g | MyClass::A; // Output 2 <?php // com/yahoo/MyClass.php namespace com | yahoo; class MyClass { const A = 1; } <?php // com/google/MyClass.php namespace com | google; class MyClass { const A = 2; }
  Only unqualified and qualified names are affected by aliases; fully-qualified names are not. Therefore, the following will not be affected by the defined aliases:
<?php namespace myLibrary|classes { require_once('myLibrary/classes/myClass.php'); use myLibrary | classes | myClass as Another; $a = new Another; $b = new | myLibrary | classes | myClass; // fully-qualified names are not affected by alias }
For convenience, PHP lets you separate multiple namespace declarations with commas.
use comyahoo as y, comgoogle as g;
To explicitly use the current namespace and avoid aliases, use the namespace keyword before the identifier name.
namespace myLibrary|classes { require_once('myLibrary/classes/myClass.php'); use myLibrary | classes2 | myClass as Another; $a = new namespace | myClass; // with keyword namespace you can avoid alias }


http://www.phparch.com/2010/03/namespaces-in-php
http://phpmaster.com/php-namespaces