Membership
Main Menu
Forum Boards
Stats
- 18 tutorials
- 72,337 members
- 696,735 forum posts
- 11 blog posts
Tutorials
OO PHP Part 1: OOP in Full Effect
Views: 38559
4. Hot off The Press Features
4.1 Namespaces
PHP 5.3 and up offers namespaces which allows you to give your classes logical names without resorting to really long winded names like the ones caused by the PEAR naming standard.
Simply put, the PEAR naming standard defines pseudo namespaces, which map to a location on the file system (see section Autoload). A name like the following:
Application_Input_Validate_PhoneNumber_Us
Would map to Application/Input/Validate/PhoneNumber/Us.php
Sometimes these names get way bigger than this. Here’s a different example, from Zend Framework:
Zend_Controller_Action_Helper_AutoComplete_Abstract
It can get even bigger.
PHP 5.3+ namespaces solves this by creating aliases.
Now, I could call this:
But that can hardly be called an improvement, can it?
Instead, knowing that I am going to use a phone number validator, I can create a namespace alias:
Better, no? It saves me having to refer to the fully quantified class name, and I can select the right validator class in my client code without littering it with repeated lengthy namespace selections.
I can also create an alias for a class name. For example:
The fully quantified name of the class includes the namespace. Hence, calling var_dump on the above $response object would output something like the following:
object(Framework::Controller::Response::Http)#1 (0) {
}
This allows us to mend our __autoload function to load classes in a namespace:
4.2 Late static binding
In PHP 5.2, you can already do this:
Static methods are inherited. But without late static binding, ParentClass will not be able to invoke any static methods of ImplementationClass, like in this non-static example:
If we wanted meh to be static, it would be tempting to try this:
But self refers to the current class scope, ParentClass in this case, so we produce this error:
Fatal error: Call to undefined method ParentClass::meh()
In PHP5.3+, self still points to the current class reference. To make the above scenario possible, a new use is given to the static keyword:
Above will echo ‘blah’ on PHP5.3+.
But, all is not as simple as it seems. The static keyword doesn’t take inheritance into account, like $this does. Instead it tries to resolve the correct call.
The following code will fail:
Because 'parent' resolves to ParentClass, 'static' in delegate() resolves to ParentClass as well. But ParentClass doesn’t have a method called ‘meh’; the dreaded fatal error we got before is inescapable. You’ll get similar results trying it with multiple levels of inheritance, a fully resolved method call (ParentClass::delegate()), or with static properties instead of methods.
In summary: if you want to use this new feature, be very aware of its limitations.
5 In conclusion
If you have any questions regarding this tutorial, please use the forums. If you want to comment you can do so below.
Thanks for reading, see you on the forums?
