WAY2WEB: Web Design & business...
7 Reasons to learn Object-Oriented PHP
Object-Oriented programming is the foundation of many languages, such as the .NET family of languages, and Ruby. In these languages, you can't work without it. However, PHP is a different story. Most beginners tutorials don't even teach Object-Orientated programming, and the young programmers have to go out of their way to learn this important aspect of programming.
Most casual PHP programmers don't even learn object-orientated programming at all! Having known and used object-orientated PHP for quite some time, I find this quite sad. Why don't they learn it? Are we all too lazy? Are we ignorant? Or is it just there isn't enough reason to switch from the procedural approach.
If you're hesitant in taking the plunge to learning object-orientated PHP, then here's a few reasons for you. When we've convinced you to take the plunge, we've also got a great tutorial for learning Object-Orientated PHP.
Reusable Code
One of the biggest advantages with object-orientated PHP is the fact that you can reuse and recycle all of the code which you write. There are whole websites setup to facilitate the open sharing of object-orientated PHP code (such as PHP Classes), where you can search for the job you want to do, and it will return some code to help you complete that job.
Of course, it isn't just open sharing of code which object-orientated PHP is good for. It's also great for reuse within your own applications. Take this scenario: you have several pages which make the same database call, and and then process the data in the same way. The procedural approach would require you to copy the code into each page – but what if you required a change? You would have to change each file manually! Object-orientated programming overcomes this issue.
Manageable Code
Procedural code is yucky. It is messy, gets tangled, and hard to read. I have maintaining my old procedural code for this very reason. Object-orientated PHP take the stress out of managing your code. When you have everything wrapped within classes, objects, and are using public and private variables the right way, everything just gets a whole heap easier.
APIs Made Easier
If you're developing a product which you intend to allow external developers to add functionality to your app, or share data with it, then chances are you are developing some sort of API (Application Programming Interface).
If you've already got all the core functionality of your program wrapped in classes, then adding an API is a simple matter of adding an extra layer (probably XML) to talk to the existing object-orientated PHP, and then wrapping some security around that XML layer.
Secure your Variables
If you're dealing with secure data, there is a chance you want some way to protect some variables while they're being processed. Introducing: private and public variables. Within a class, you can define variables to be private (meaning not readable outside that class), or public (can be read outside the class).
You can now be a little bit more certain that you're sensitive data won't be leaked out of a class from where they shouldn't be. This is handy when you're dealing with encryption, and don't want to have the encryption keys leaked out, etc.
Be ready for Database Changes
Changing database platforms can be a nightmare! You just finish writing the software to interface with Microsoft SQL Server, and now your clients want it to also work with the superior MySQL database. Procedural code would require you to go through every file, and change the native database function calls.
With object-orientated programming, you should have all of these database calls within the one class, from where you can easily change them, or move them into a database abstraction layer.
Separate PHP from HTML/Javascript
One of the biggest sins in PHP development must be the integration of HTML code with your business logic! Having all your formatting tags within the logic is bad programming practice, and should be avoided. Don't do it. Ever. I'm serious!
It's like selling breakfast cereal in boxes with the milk, strawberries and bananas already in there, mixed together. Why if you don't like the bananas? It's too late to pick them out – the same is true with mixing PHP and HTML together.
Tempting systems such as Smarty allow these two components to be separated, making them both easier to change. This also allows for template caching, to speed up your applications. Of course, Smarty, and all other half-decent templating systems are based on object-orientated programming - if you know object-orientated PHP, the learning curve is minimal.
Use a framework
Frameworks are the magic of the programming world. They speed up your application development by already having common tasks written, and available at the tip of your fingers. Many companies insist frameworks are used so you can be more productive when programming. It makes sense from a business perspective.
Of course, if you know object-orientated PHP, then learning a framework is simply a matter of reading the framework's manual, and using the functionality which the framework provides you with!
Ready to take the plunge: here's how to learn!
So, we've given you seven practical reasons to learn Object Orientated PHP. Why wouldn't you want to learn? To make the learning process as easy as possible for you, we have a guide which you can learn from: Understanding Object Orientated PHP. There's also plenty of guides on the internet which will help you in the learning process, so make sure you utilise them to your maximum advantage.