TomB
February 21, 2013, 9:07am
10
I suppose it depends what you mean by “Cool things”. From going to intermediate to advanced, really you’re looking at design patterns, best practices and advanced OOP theory. None of which are really PHP Specific.
Rather than repeat myself, I’ll repost my thoughts on the subject from here: http://www.sitepoint.com/forums/showthread.php?964512-Better-PHP-Development&p=5304801&highlight=#post5304801
It’s a long process that only comes with practice. Once you move beyond asking “How can I solve this?” to “How should I solve this?”.
In general, I’d say applying a couple of principles to your code will demonstrate to you how to code is better. It’s not immediately obvious what advantages they give until you’ve used them in real projects. The benefits come to you when you try to use your code and realise how flexible and easy to maintain it is.
Favour composition over inheritance. I actually removed 99% of “Extends” keywords from my codebase and it became a hundred times better. It wasn’t until I did it that I realised quite how much I was coding to work around inheritance and using it where I really shouldn’t be.
Make all class variables private and avoid getters/setters unless they are absolutely needed. This goes hand in hand with 1) but it will help you achieve better encapsulation, which is a fundamental principle in OOP.
Dependency inejection. Once I started using a dependency injection container, I realised that most of the code I had was messy. Creating objects arbitrarily is messy! Try IoC (inversion of control) and you’ll quickly see the benefits.
Law of demeter: http://en.wikipedia.org/wiki/Law_of_Demeter Apply this and your code will become better. It’s not immediately obvious if you don’t understand it, but it forces you to think about the dependencies your classes have.
Unit testing. Once you start writing code that’s easy to test, you have code that’s easy to isolate and therefore, reuse.
That’s my top 5 tips for going from “advanced” to “pro”.