mechanism <?php namespace MyNamespace; // I'm in "MyNamespace"! The namespace keyword, followed by an actual name, declares the namespace of the code that follows.
mechanism A namespace must be the first line of code in a file. Unless you have multiple namespaces in one file... <?php namespace MyNamespace; // I'm in "MyNamespace"!
mechanism A namespace must be the first line of code in a file. Unless you have multiple namespaces in one file... <?php namespace MyNamespace; // I'm in "MyNamespace"!
belong to that namespace. <?php namespace MyNamespace; class MyClass { public function __construct() { // do something! } } The full name of this class is: MyNamespace\MyClass
your FATAL ERROR! $object = new MyNamespace\MyClass(); // enjoy your awesome class! Classes must be accessed using the full namespace path Backslash (\) is the namespace path separator operator thing. Backslash (\) is also the escape character. I hope this feels as awkward for you as it does for me...
PHP 5.3, every class, function, variable, constant, etc is located within a namespace, even if you don't use them. ▪ If it isn't explicitly in a namespace, it is in the global namespace (or global scope). ▪ The global namespace in PHP is called: \
internal PHP classes are located outside of your namespace. To access them, you must precede them by their namespace name (\). <?php namespace Foo; class Bar { public function __construct() { $element = new \SimpleXmlElement('<bar/>'); } }
new MyClass(); $object->setOther(new OtherClass()); The use keyword lets you import a resources from a namespace into your current scope. This helps keep classnames short. A good IDE will generate the use statements for you.
$object = new MyClass(); $otherObject = new MyOtherClass(); Classes in namespaces can be aliased Useful when two classes in different namespaces have the same name.
new Mysql(...); Bonus Round! Aliasing is also helpful with really long class names. I'm looking at you Zend Framework 1! * * I know, they couldn't help it. Namespaces weren't around yet...
the way in which you should setup your namespaces. ▪ A namespace must begin with a Vendor Name ▪ The second part should be the name of the package ▪ Further hierarchy is up to the developer, but usually is grouped by component. Example: namespace Doctrine\ORM\Mapping;
should map 1 for 1 with your directory structure in your source code directory. Namespace: namespace Doctrine\ORM\Mapping; Source Code: /path/to/MyProject/src/Doctrine/ORM/Mapping /Annotation.php /Column.php
autoloader interoperability. This means that, when following PSR-0, resources in your source or library can be autoloaded like any other PSR-0 conforming library. It also makes package managers like Composer work well. Learn more: https://github.com/php-fig
organization of your code ◦ Aids in separation of concerns by giving "packages" ◦ Allows use of shorter class names ▪ Easily use 3rd party libraries without conflict (aliasing) ▪ Provides a common format for autoloader interoperability (PSR-0)
static function foo() { return "foo"; } public static function announce() { self::foo(); } } <?php class OtherClass extends SomeClass { public static function foo() { echo "bar"; } } <?php echo OtherClass::announce() . "\n"; $ php test.php foo that's not really what I wanted...
static function foo() { return "foo"; } public static function announce() { static::foo(); } } <?php class OtherClass extends SomeClass { public static function foo() { echo "bar"; } } <?php echo OtherClass::announce() . "\n"; $ php test.php bar So much awesome!
totally access an inherited, overridden method or variable. ▪ The self keyword refers to whatever class it is used in, regardless of inheritance. ▪ The static keyword refers to whatever class it is called on, and is evaluated when called, instead of where declared. That being said, limit your use of static classes. They can make testing your code more difficult.
You can only inherit from one class. ▪ Sure, you can implement oodles of interfaces, but you still have to rewrite the implementation several times. ▪ This is not DRY. ▪ PHP 5.4 solved this problem by introducing traits.
protected $firstName; protected $lastName; public function setFirstName($firstName) { $this->firstName = $firstName; } public function getFirstName() { return $this->firstName; } public function setLastName($lastName) { $this->lastName = $lastName; } public function getLastName() { return $this->lastName; } }
of using the array() language construct! $users = array('Bob', 'Sue', 'Frank', 'Thomas'); $users = ['Bob', 'Sue', 'Frank', 'Thomas']; This. Is. So. Awesome.
methods!) that return arrays can now use the result immediately, without having to store in a variable. <?php function getUser() { return ['lastName' => 'Cabrera', 'firstName' => 'Miguel' } echo getUser()['lastName'] . "\n"; Variables are so 2012.