Book Image

Object-Oriented Programming with PHP5

By : Hasin Hayder
Book Image

Object-Oriented Programming with PHP5

By: Hasin Hayder

Overview of this book

<p>Some basic objected-oriented features were added to PHP3; with PHP5 full support for object-oriented programming was added to PHP. Object-oriented programming was basically introduced to ease the development process as well as reduce the time of development by reducing the amount of code needed. OOP can greatly improve the performance of a properly planned and designed program.</p> <p>This book covers all the general concepts of OOP then shows you how to make use of OOP in PHP5, with the aid of an ample number of examples.</p>
Table of Contents (15 chapters)
Object-Oriented Programming with PHP5
About the Author
About the Reviewers

Difference of OOP in PHP4 and PHP5

Objects in PHP5 differ a lot from objects in PHP4. OOP became matured enough in true sense from PHP5. OOP was introduced since PHP3 but that was just an illusion for real object oriented programming. In PHP4 you can create objects but you can't feel the real flavour of an object there. In PHP4 it was almost a poor object model.

One of the main differences of OOP in PHP4 is that everything is open; no restrictions about the usage of methods or properties. You can't use public, private, and protected modifiers for your methods. In PHP4 developers usually declare private methods with a double underscore. But it doesn't mean that declaring a method in that format actually prevents you from accessing that method outside the class. It's just a discipline followed.

In PHP4 you can find interfaces but no abstract or final keyword. An interface is a piece of code that any object can implement and that means the object must have all the methods declared in the interface. It strictly checks that you must implement all the functions in it. In the interface you can only declare the name and the access type of any method. An abstract class is where some methods may have some body too. Then any object can extend that abstract class and extend all these methods defined in that abstract class. A final class is an object which you are not allowed to extend. In PHP5 you can use all of these.

In PHP4 there are no multiple inheritances for interfaces. That means an interface can extend only one interface. But in PHP5 multiple inheritance is supported via implementing multiple interfaces together.

In PHP4, almost everything is static. That means if you declare any method in the class, you can call it directly without creating an instance of it. For example the following piece of code is valid in PHP4:

class Abc
  var $ab;

  function abc()
    $this->ab = 7;
  function echosomething()
    echo $this->ab;

echo abc::echosomething();

However it is not valid in PHP5 because the method echosomething() uses $this keyword which is not available in a static call.

There is no class-based constant in PHP4. There is no static property in objects in PHP4, and there is no destructor in PHP4 objects.

Whenever an object is copied, it is a shallow copy of that object. But in PHP5 shallow copy is possible only using the clone keyword.

There is no exception object in PHP4. But in PHP5 exception management is a great added feature.

There were some functions to investigate methods and properties of a class in PHP4, but in PHP5 beside those functions, a powerful set of API (Reflection API) is introduced for this purpose.

Method overloading via magic methods like __get() and __set() are available in PHP5. There are also lots of built-in objects to make your life easier.

But most of all, there is a huge performance improvement in PHP5 for OOP.