My notes and thoughts about Linux, WordPress, PHP and many more.
This is a complete beginner tutorial on PHP object oriented programming. This tutorial assumes you do not have any single knowledge about PHP – a pure beginner in PHP. As a start, OOP is the best way to write programs in PHP. It is considered as one of the best practices of coding PHP. So if you are a beginner, stick this to your mind. This is just the first part, php-developer.org plans to publish a lot of tutorials dedicated to object oriented programming.
This is not to say that if you are not writing code in OOP method, its wrong. It is still acceptable but it is not recommended for long-term professional practice of PHP programming. Why bother writing in object oriented style? Below are the following reasons of the main advantages of object oriented programming:
1.) Modular – it is very easy to manage codes in terms of functionality and features if you arrange your codes in terms of modular blocks. You can only implement this type of blocks in an object oriented programming.
2.) Cleaner code and more organized looking code – since you have arranged and categorized your blocks of code, it looks cleaner overall. You can easily locate any functions, etc. since the code is well organized. Any novice and even beginner PHP programmer with adequate background on object oriented programming can easily understand what’s going on with your code.
3) Portability– imagine if you are not writing PHP code in object oriented style. What will happen is that if someone will manage to rewrite your code; it will be very difficult because its not organized and not modular. With object oriented style of writing code, you can deploy your codes anywhere and let anyone handle it with ease. It is the primary reason why most content management systems and PHP frameworks (those that are used by big websites) are coded in object oriented style. The primary reason is that there are a lot of developers involved and if the code are not organized and modular, chaos will result during any improvement plans or code updating.
4.) Scalability – imagine a PHP code which is not object oriented; this is called procedural method. The code instructions in a non-object oriented approach are entirely executed from top to bottom and all are placed in a single PHP file. Supposing that a script is designed to accomplish something. Now what will happen is that if you add more features to the script; there will be chaos. Why? Since the code is written in a top to bottom approach; adding even a single feature can affect the runtime/code execution. It will be VERY difficult to update the programs. There will be occasions where, the entire rows of the program will be REWRITTEN just to add that single feature. This is a waste of time.
With object oriented programming- there is always scalability. It is because features/functionality of the main script are grouped as modules or blocks. If you add another feature, it is as simple as writing a new module and incorporating it to the main script. Giant and powerful websites in PHP are done using this approach. Take for example Facebook; which is a website written in PHP. This is one giant website with millions of visitors per day. It also demands a lot of features and lot of improvements to be done on a daily basis. Without scalability, Facebook cannot compete with the rest of the social networking websites. If features are to be added, Facebook engineers would simply write a single object oriented module and add it to their site main script.
Below is a diagram that compares traditional programming approach (procedural) vs object oriented programming. An illustration of a main project script (mainscript.php) with a feature to add two numbers:
As you have observed, the traditional programming approach in PHP is nothing but a sequential top to down code execution on a single file. Meanwhile on an object oriented programming, a module called as sumclass.php is created with a primary purpose to to add numbers. In this module, the number variables are defined as well as the setting, getting and summing functions. The sumclass is in then referenced in the mainscript.php so that it will have a feature to add numbers. The summing function of this class is then invoke and the results are returned.
As you can see, inside the dotted box is a block of code in your main script that add numbers. And this block of code is even simplified by putting all the details in sumclass.php. This is why object oriented programming is neat and organized. Now if you plan to add a new feature like multiplying numbers; it looks very hard to incorporate on a sequential approach because the entire code is executed line after line. But in an object oriented method, this is very easy to incorporate. The programmer will simply write a new class called multiplyclasss.php and then invoke the multiplication functions to the main script. There you have it – a new feature of your main script is added. Though object oriented programming looks scary at the start (particularly for a single feature script); as you add more feature this looks very easy and simple. It now appears that the procedural method is much scarier to organize, maintain and harder to troubleshoot. Since most PHP web application can be quite complex (lots of features and functionality), this is why OOP became a best practice in coding PHP.
In the coming tutorials you will learn more about PHP classes, objects, methods, properties, etc.