Tutorials

OO PHP Part 1: OOP in Full Effect

by John Kleijn on Jun 6, 2008 3:21:29 PM

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?

Comments

Looks like a nice intro loaded with information. Some things I see lacking in OOP with PHP is the use of get and set methods. When learning OOP in java this was a key thing we learned I just think it makes good practice.

1. Jeff Combs on Jun 6, 2008 8:17:36 PM

I agree wholeheartedly, but as it says in section 1.2: this tutorial only shows the language features.

Good practice will be a subject soon enough.

2. John Kleijn on Jun 7, 2008 3:43:41 AM

I can't wait for the rest of the series, I've been struggling to learn good OOP Application design for some time because I don't have any peers where I work who know the subject, given this first part I think I will learn a lot and really appreciate the author's efforts. Thank you!

3. phpzone on Jun 7, 2008 8:51:58 AM

You're welcome (though I can never have enough of gratitude). ;)

Note that part 2 is already online, and part 3 underway.

4. John Kleijn on Jun 7, 2008 11:51:03 AM

lovin this. :D

5. aim25 on Jun 22, 2008 11:46:05 AM

Cool tutorial, really useful and easy to understand

6. Jack Bert-Oswald on Jul 15, 2008 11:04:27 PM

Thanks man, really helpful. Tutorial is great but i think that would be great to add some more explaining on interface if you have time cauz i really didn't understand what should i do with the code you give as an example. Thanks for effort to make this tutorial.

7. HardCoreMore on Jul 18, 2008 2:44:31 AM

From 2003 I am great follower of php tutorials by phpfreaks. It's very useful the stuff on advanced. These kind of tutorials make us strong in fundamentals.

Good work.
sharma chelluri

8. sharma chelluri on Aug 1, 2008 8:52:31 AM

Great tutorial, but honestly, we're not programming PHP animals. In future tutorials, could you cut the crap about the dog and provide code that does something meaningful?

9. sneamia on Sep 13, 2008 5:12:13 PM

As a beginner I like analogies that make it easy to follow.

10. arwvisions on Oct 4, 2008 11:16:59 PM
Login or register to post a comment.