Categories
GNU/Linux Free Software & Open Source Programming & Web Development

PHP 5 OOP implementation quirks

PHP 5 is supposed to be fully object oriented. Yes, it has classes and inheritance and all those things an OOP language is supposed to have. But, is it implemented like most OOP languages?

First of all, I come from a C/C++ background, so there’s where I got all my OOP lessons. Then I started to learn Python and Ruby, and well, finally lots of little OOP details I couldn’t understand were clear. Ruby has a beautiful OOP implementation, sometimes it can get confusing having everything as an object and the object and class themselves are objects. But once you pass that, everything is cystal clear.

Constructors

What bothers me about PHP 5’s OOP implementation is first of all, constructors. If I declare a Parent class, with a constructor, then a Child class that inherits from Parent, and this Child class has its constructor, when I instantiate an object of the Child class, only the Child class constructor is called.

class Parent
{
  __construct()
   {
      echo "hello from parent ";
   }
}
class Child extends Parent
{
 __construct()
   {
      echo "hello from child ";
   }
}
dummy = new Child();

This will display only

hello from child

Where in other languages, such as C++, Python or Ruby (don’t know about Java, but I expect the same) the output would be:

hello from parent hello from child

So in order to have this constructor cascading effect, in PHP 5 I have to explicitly call the parent constructor on my child’s constructor like this:

class Child extends Parent
{
 __construct()
   {
      echo "hello from child ";
      parent::__construct();
   }
}

Which is not very nice.

Accessor methods

Another aspect that I dislike is the class attribute accessor methods. I call them accessors like in Ruby, but they are commonly called setter and getter methods. So in order to control and filter my input/output for my object, I usually do so with this methods.

If I declare a User class with some methods, I expect the setter and getter methods to filter my I/O everytime I want to access the class’ attributes.

class User
{
   public $name;
   public $email;
   public $password;
 //set and get functions
  public function __get($variable)
    {
      return $this->$variable;
    }
  public function __set($var, $value)
    {
      trim($value);
      if($var != "password")
        {
          $this->$var = $value;
        }
      else
        {
          $this->password = hash('md5',$value);
        }
    }
}

This is supposed to encrypt the password everytime I set something on my password object attribute. What is wrong with this? That it doesn’t work! The get and set functions only work when you have NO attributes or undefined attributes in the class. So in order for this getter and setter methods to really filter I/O to the instantiated objects is to not declare the attributes, and this methods will generate them, like the PHP people say, “magically”. Maybe its a nice flexibility feature to have attributes generated on-the-fly, but it doesn’t help on object consistency.

PHP 5 is a nice tool, but as a language I think there’s still work to do on the OOP implementation.

By Gabriel Saldaña

Gabriel Saldaña is a web developer, photographer and free software advocate. Connect with him on and Twitter

9 replies on “PHP 5 OOP implementation quirks”

I agree that PHP5 OOPS is quirky… but I believe they will get better with PHP6… I am waiting to try their new features….

I have written a complete slew of articles on PHP5 Tutorials on my site and in the process of doing so have discovered numerous bugs/glitches with PHP5. Like the bug with clone(), memory references, etc.

Compared to Java, PHP5 does not seem to be strong… but they have come in a long way from PHP4…

I started my career with PHP 4, which I liked it, I mean, it was easy for what i wanted to do, when my career started to get serious and my company needed to deliver huge enterprise applications, we had to move to a OOP language, so we chose java and c#, since then i work with those two every day and I love them both. Now that I am working on a personal project with PHP 5, I don’t feel comfortable, OOP in PHP 5 doesn’t seem real to me. So I came to a decision to use PHP as it is, a web scripting language.

Have a nice day

Wait a second there… If I recall correctly, python does the same (overloads the constructor, does not inherit):

class Father:
def __init__(self):
print “Hi father ”

class Son(Parent):
def __init__(self):
print “Hi son ”

x = Son()
Hi son

While Java does it correctly (it inherits the parent constructor, then executes the child constructor):

public class Father
{
public Father()
{
System.out.println(“Hi father “);
}
}

public class Son extends Father
{
public Son()
{
System.out.println(“Hi son “);
}
}

public class MyMain
{
public static void main(String[] args)
{
Son x = new Son();
}
}

java -cp . MyMain
Hi father
Hi son

[…] el. Y que comence a usar un framework de PHP llamado CodeIgniter, del cual por ahora me reservo mis comentarios para un post mas […]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.