Object Oriented Programming in PHP - OOP Programming in PHP (Page 5 of 7 )
Some "purists" will say PHP is not truly an object oriented language, which is true. PHP is a hybrid language where you can use OOP and traditional procedural programming.
For large projects, however, you might want/need to use "pure" OOP in PHP declaring classes and using only objects and classes for your project. As larger and larger projects emerge the use of OOP may help.
OOP code is easy to maintain, easy to understand and easy to reuse. Those are the foundations of software engineering. Applying those concepts to web based projects is the key to success in future web sites.
Advanced OOP Techniques in PHP
After reviewing the basic concepts of OOP I can show you some more advanced techniques:
PHP doesn't support persistent objects; in OOP persistent objects are objects that keep its state and functionality across multiple invocations of the application. This means having the ability to save the object to a file or database and then load the object back. The mechanism is known as serialization.
PHP has a serialize method which can be called for objects, the serialize method returns a string representation of the object. However serialize saves the data members of the object but not the methods. In PHP4 if you serialize the object to string $s, then destroy the object, then unserialize the object to $obj you might still access the object methods! I don't recommend this because:
The documentation doesn't guarantee this behaviour so in future versions it might not work.
This might lead to 'illusions' if you save the serialized version to disk and exit the script. In future runs of the script you can't unserialize the string to an object and expect the methods to be there because the string representation doesn't have the methods.
You can serialize associative arrays and arrays to save them to disk too.
$obj=new Classfoo(); $str=serialize($obj); // Save $str to disk
//...some months later
//Load str from disk $obj2=unserialize($str)
You have the data members recovered but not the methods (according to the documentation). This leads to $obj2->x as the only way to access the data members (you have no methods) so don't try this at home.
There're some ways to fix the problem but I leave it up to you because they are too dirty for this neat article. Full serialization is a feature I'd gladly welcome in PHP.
Using Classes to Manipulate Stored Data
One very nice thing about PHP and OOP is that you can easily define classes to manipulate certain things and the call the appropriate classes whenever you want.
Suppose you have a HTML form where the user selects a product by selecting its product ID, you have the data of the products in a database and you want to display the product, show its price, etc. You have products of different types and the same action might have different meanings for different kind of products.
For example showing a sound might mean playing it while for some other kind of products might mean to display a picture stored in the database. You might use OOP and PHP to code less and code better:
Define a class product, define which methods the class should have (example display), then define classes for each type of product which extends the product class (class SoundItem, class ViewableItem, etc.), override the methods you define in product in each of this classes make them do what you want. Name the classes according to the "type" column you store in the database for each product a typical product table might have (id, type, price, description, etc.).
Then in the processing script you can retrieve the type from the database and instantiate an object of the class named type using:
$obj=new $type(); $obj->action();
This is a very nice feature of PHP, you might then call the display method of $obj or any other method regardless the type of object you have.
With this technique you don't have to touch the processing script when you add a new type of object, just add a class to handle it. This is quite powerful, just define the methods all objects regardless of its type should have, implement them in different ways in different classes and use them for any type of object in the main script, no ifs, no 2 programmers in the same file, eternal happiness.
Do you agree programming is easy, maintenance is cheaper and reusability is real now?
If you command a group of programmers it's easy to divide the tasks, each one might be responsible for a type of object and the class that handles it. Internationalization can be done using this technique, apply the proper class according to a language field selected by the user, etc.