Object Oriented Programming in PHP5 (Part I)
May 7th, 2008PHP6 is coming closer and closer and it will enrich the famous programming language with lots of cool features. One of the areas that will benefit the most from this new release is the Object Oriented Programming (OOP) part. This area has developed in an accelerated manner so the language can catch with other programming languages like Java, C++ or C#.
Looking back in time at PHP4, we see a relatively weak programming language when it comes to OOP. PHP4 implemented support for classes, inheritance and object serialization methods, but that was kinda it. Of course, it had a few other small features (like the parent variable inside each class), but most of the important ones were missing and made developing famous design patters like Singleton or Factory impossible.
But then came the knight-in-shiny-armor called PHP5 to the rescue. Most of the needs of programmers were covered with this release.
In this article, we will take a look at the most important (and useful at the same time) features that were added into PHP5.
For starters, let’s mark the changes:
1) Constructors
Constructors used to be declared as a function with the same name as the class’. For example, if you had a class called MyClass, you would have to create a method called MyClass(), which would be used as a constructor. The problem with this approach came when you wanted to change a class’ name: you would have to remember to change the name of the constructor each time.
In PHP5, however, the way the constructors are declared has changed, so that it is independent of the class name. To declare a constructor in PHP5, just declare a method called __construct() inside the class.
For backwards compatibility, you can still use the old constructor style in PHP5, but it is discouraged.
Note: Neither PHP4 or PHP5 call the parent constructor from the constructor of the child class. It is up to the user to do this. You can do it using the resolution operator (Paamayim Nekudotayim) like this: parent::__construct() or parentClassName::__construct().
2) Destructors
Destructors were not available in PHP4, but with PHP5 they can now be invoked using the special magic method: __destruct().
For the sake of consistency, you should try declaring constructors and destructors using PHP5 style. (Never use the PHP4 style constructor and the destructor provided in PHP5).
Now, let’s get to the really nice part: the new features of PHP5.
1) Visiblity property
This was one of the key features missing from PHP4: you could not declare private or protected members inside a class. They were all public.
However, in PHP5 you can safely use these properties and be sure they will work the right way. The available keywords are (according to the official PHP5 manual):
- public: The resource can be accessed from any scope.
- protected: The resource can only be accessed from within the class
where it is defined and its descendants.
- private: The resource can only be accessed from within the class
where it is defined.
- final: The resource is accessible from any scope, but cannot be
overridden in descendant classes.
Examples:
If you want to have a protected variable inside a class, just declare it like this: private $var = ‘12′;
If you want a protected function, here is how to do it: protected my_function()
Please not that you must specify the visibility before each member of the class (the implicit state is public), unlike in other languages, where you could group several members with a single keyword.
2) Autoloading
Sometimes, it is quite time-consuming to load all the classes you could possibly need in your application and it would be a lot faster if you could load only the ones you need. PHP5 comes with a solution to this: the __autoload() function. The function takes one parameter, which is the class name.
Here is a sample definition of such a function:
function __autoload($className) {
require_once $className . ‘.class.php’;
}
3) Static members
This is a key feature in developing famous design patterns like Singleton or Factory. Basically, having a static member inside a class means that it will be accessible without an instance of that class.
Please note that the $this variable is not available inside methods declared as static.
Using the 1) and 3) additions to PHP5, we can now implement a Singleton sample class:
class MySingleton
{
static $instance;
private $data;
private function __construct($data) {$this->data = $data;}
private function __clone() {}
public static function getInstance()
{
if ($instance instanceof MySingleton) {
self::$instance = new MySingleton(23);
}
return self::$instance;
}
}
These were only a few features that come with PHP5 and look what a splendid class we built using them: one that can be instantiated only once! Any other attempt to get an instance of it will return the already existing instance. Neat, huh? Stay tuned for part two of the tutorial, I will show you some even cooler stuff!
Daia Lucian