Objects and PHP: A brief evolutionary history


Objects were not a part of PHP in its earlier form. They weren’t even planned to be. PHP’s developers describe it as an afterthought. Objects made their first presence in PHP 3. PHP 3 was a complete rewrite of PHP 2 (then called PHP/FI 2.0). Here is how it happened:

  • PHP/FI 2.0 (Personal Homepage Tools/Form Interpreter): Not the PHP we know today
  • PHP 3: Objects appeared in PHP
  • PHP 4: Object Oriented Programming was significantly improved
  • PHP 5: Objects as the driving force

PHP 3:

Classes and Objects were introduced for the first time in PHP but they were just another way to access associative arrays. They were not intended to be a part of PHP 3 at the first place. Despite being able to define methods and inheritance, there were severe limitations to OOP. The documentation was also very poor in helping users understand PHP’s object-oriented capabilities.

PHP 4:

With PHP 4 came a major rewrite of PHP’s core, the Zend Engine. Zend engine is what drive PHP behind the surface. This engine is what is responsible for all the database talks and other things PHP is supposed to do. Support for Objects was significantly improved. There were now ways to access overridden methods of parent class (if you don’t know what that means don’t worry, we’ll talk about it later), more support for classes but there were still significant drawbacks. A major drawback was the default pass-by-value behavior when assigning objects to variables or passing them to functions. Meaning, the following code will result a copy being made of the Car object.

$car = new Car();
$newVar = $car;

This would result in two Car objects being created. This default behavior resulted in a lot of bugs as it was easy to lose track of changes being made to the object in different parts of the code.

PHP 4 did provide a way to pass objects by reference.

$newVar =& $car;

In case of functions:

Pass argument by reference

function foo (& $car){
//code
}

Return by reference

function & foo (){
return $this->car;
}

It was however very easy for a developer to forget a tiny ampersand.

Nonetheless, PHP 4’s Object Oriented Support and good documentation was well received. Zend started recommending developers to use Objects Oriented approach rather than procedural one, forums were full of questions like “do I need OOP?”.

The interest in OOP grew fast. PHP’s official software repository itself was filled with great OOP and design pattern implementation.

PHP 5:

With PHP 5, objects were at the center of everything. They are now recognized as a powerful tool for enterprise level applications and drive the language. Make a note though; PHP is still a language that supports Object Oriented Development. It is not an Object Oriented Language. It still supports procedural programming. Object Oriented Design is however much more powerful and hence widely accepted.

PHP5 supports pass-by-reference by default when it comes to object. PHP 5.3 and recently release PHP 5.4 have added those few powerful OOP features that were missing in PHP 5. Features like Namespaces and Variable Static Binding were added in PHP 5.3. A new and very powerful feature “traits” was introduced in PHP 5.4.

PHP 6:

PHP 6 has not been released and it is expected to take some good time. It is still in development phase. The major expected update in PHP 6 is native support for Unicode string handling. However it is this feature only that has kept us waiting for so long.

The future of PHP is surely packed around object oriented design. Next few articles will introduce you to objects and classes.

Advertisements

4 thoughts on “Objects and PHP: A brief evolutionary history

  1. If OOP is becoming the focus of PHP, one has to wonder… is PHP slowly evolving into Python? And if so, why not just use Python? I ask this in all seriousness. If PHP development is focussed on grafting a great object system onto the language, they may succeed, but they will essentially end up with an imitation of Python, which was object-oriented from the start. Surely there are things that PHP does better than any other language – and it seems to me that pushing those forward ought to be the direction that development takes.

    • PHP’s development is not meant to ultimately become an all OOP language. From the start PHP’s main agenda has been to be a great, powerful and easy language for the web. PHP’s agenda has never been to force a developer to adopt object oriented paradigm, unlike python. Both are very powerful language but at its core they can be said to follow different ideologies. PHP has evolved considerably around OOP for OOP offers more power when it comes to enterprise scale applications. But not everyone is developing that kind of software. Any novice can create a feature-full website without using OOP, by creating just a bunch of PHP functions. It is because of this vary reason that more than 70% of all the websites are powered by PHP. As it doesn’t require one to be an expert to create something.

      Even today the debate of OOP vs Procedural Paradigm is popular among developers and PHP can be said to support arguments from both side.

      *Correcting myself: Python doesn’t force a user to adopt OOP as well but the language has been designed to be OOP centric in most part. Also, Python is a programming language while PHP is a server-side scripting language.

  2. Pingback: A geek with a hat » My brain can’t handle OOP anymore

  3. Pingback: Introduction to Classes, Class Declaration and Objects « teknonics

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s