website stat

Object Oriented paradigm in PHP5

I didn’t see any review or news regarding the changes to the Object Oriented paradigm in PHP5 so I decided to dig a little bit on the subject. Actually, I had already found out that I was missing some new things PHP5 has brought. I started using PHP5 due to the SimpleXML parser so I didn’t pay much attention to the news.

But definitely version 5 has brought plenty of new things. Let’s take a look.

Variable, method/function and class scopes have been introduced. The private, public and protected scopes are available. So as constructors and destructors. Let’s see how these two concepts, scopes and constructores, are used in version 5:


public class MyClass {
private $attrib1;
public $attrib2;

public function __construct($attrib1) {
$this->$attrib1 = $attrib1;
}

public function __destruct() {
}
}

Another important thing, not related to Object Oriented but inherited from the OO programming practices: a file per each class. On PHP there’s a mechanism called auto-loading that provides precisely this feature. Automatically loading a class without having to do an include for each file.


function __autoload($class_name) {
require_once $class_name . ‘.php’;
}

There’s also the definition of static that keeps its value between class instantiations (objects). Static fields are accessed this way:


MyClass::$static_field;

This allows implementing a very important design pattern, Singleton.

Another concept introduced in PHP 5 are the abstract and interface definitions. It’s usage is pretty basic:


abstract class AbstractClass
{
// Force extending class to define this method
abstract protected function getValue();

// Common method
public function printOut() {
print $this->getValue() . “\n”;
}
}
interface iTemplate
{
public function setVariable($name, $var);
}

Iterators is a somewhat confuse matter. They are a design pattern, so every language would technically support them. But they only make sense when they’re implemented on the common data structures, namely Hashtables, Vectors, Lists, etc.. Since PHP does not have these structures implemented by default, you can’t really have iterators over them :-) Obviously, the reader is invited to implement them and share the code. I believe they’ll make part of Zend PHP framework.

You now have the chance to force a variable type. That’s called type hinting. On previous versions of PHP, a variable didn’t had a type. You could just start using it. I don’t know about you but this behaviour has brought me lots of problems and made me waste a lot of time. By default, PHP does not print notices which report this kind of problems. You have to explicity enable them on php.ini. So, if you try to compare oranges with apples no error will raise. Your application will simply not work as excepted. This way you’ll be able to force error parsing and it will also enforce better code.

To finish this review, there are the long so waited exceptions. They are implemented pretty like Java so one could just do


try {
// code that may throw an exception
}
catch (Exception $e) {
// handle the exception
}

As far as I know, the finally clause is not yet available.


2 Responses to “Object Oriented paradigm in PHP5”

  1. David Ramalho
    Published at December 9th, 2005 at 8:34 pm

    And Class Constants and the Final clause :) Yes, PHP has reached almost full fledged OO, which was THE reason for me sticking badly to it :) , and can’t really wait for PHP 6.0 and bloody helpfull namespaces :).
    Cheers Mario ;)

  2. mlopes
    Published at December 9th, 2005 at 10:09 pm

    That’s it, basically PHP ownz!

    Cheers David! :-)