Get your FREE 30 page Developing SOLID Applications guide!

Mastering Object Oriented PHP

Object oriented PHP can leave you with quite a headache. It’s a challenge. A struggle. Something that you fight against. The principles seem abstract, and you wonder whether or not object oriented PHP can ever be something that you can grasp and be good at. Object oriented PHP is obtuse and hard to follow. You wish it was easier to understand.

I understand how this feels. Back when I started writing PHP, there wasn’t much of an object model to behold. Those were the “good old PHP 4″ days, when an object was little more than an array with methods slapped on it. Objects didn’t have visibility, there were no interfaces, constructor functions were just functions inside the class with the same name as the class, and object oriented principles didn’t really apply.

PHP 5 has been out for a long time now. But still, I come across programmers that have a hard time understanding the PHP 5 object model. They either learned the PHP 4 object model and never really grew their skills, or due to the fact that PHP was essentially “backwards compatible” for a long time, the old bad habits never got corrected.

The truth is that mastering object oriented PHP is a challenge, because a ton of bad information is out there about the PHP object model and how it works. I believe much of this disinformation has a root in the “sins of PHP 4″. It can be really hard to find reliable information.

Yet it is possible for you to master object oriented PHP.

This is a big part of why I wrote Mastering Object Oriented PHP. My goal was to create a book that would be a go-to resource for developers who understood the basics and wanted to reach the next level in their object oriented development practices. I was tired of seeing books that simply recycled the documentation, were written for C or C++ programmers, or were completely theoretical and didn’t actually help developers do their jobs.

The response has been phenomenal. I’ve heard that when it comes to writing a book that expresses how to be great at object oriented PHP, I nailed it. Anthony Ferrara told me that “Until now, there wasn’t a single object oriented PHP book that I could recommend (mainly due to them all being garbage). I’m glad to finally have one!” I was thrilled to hear this from Anthony, because I really wanted to help people master object oriented PHP through the book.

Over the weekend I released a new version of Mastering Object Oriented PHP, with bug fixes and new content on what it takes to truly become a master. The new version is better than ever, and I’m really excited about it! If you’ve struggled with object oriented PHP and want to write better PHP code, check out the new version. You won’t be disappointed!

Stop struggling with object oriented PHP. You can write better object oriented PHP code today. Get your copy of Mastering Object Oriented PHP today!

Write better object oriented PHP today.

Object oriented programming always leaves you with a headache. What if you could master it instead?

Get the book now! »

KMB wrote at 1/15/2013 2:32 am:

Imho what OOP in PHP is lacking is a good reason to use it. At least for me. I understand the basics but I am missing one or two good arguments to use it (which would then obviously give me enough incentive to buy your book ;-).

Is it better performance-wise? What’s the advantage over procedural programming besides the “capsules”, which makes using duplicate function-names easier?

The last question could also be described as: On one side I have a nice little function with some arguments I can easily use in the rest of my code-base. On the other side I have to define classes/objects apply a bunch of “uninteresting” functions like setters and getters and suddenly my code-base has expanded manifold.

Please note, that I don’t have to share my code-base with anyone and I am the only one working on it, so there is seldom a need for “capsulating” code.

Brandon Savage (@brandonsavage) wrote at 1/15/2013 7:33 am:

For years, PHP was largely a procedural language. Object orientation began with PHP 4, and improved in PHP 5. There are a number of reasons why OO PHP is better than procedural; I spend a good bit of time discussing that in my book. The short list is that it makes it easier to test, easier to swap in and out different components, and simpler to maintain.

While you may not share your code base now, I’d bet you that if you’re pursuing PHP professionally in the next five years you will share a code base with somebody. And that will make a huge impact in your thinking process.

As for “getters and setters”, PHP doesn’t require that you define them and doesn’t define them automatically. That’s purely a style thing and you can access object properties with $object->property as long as they are public.