Ask HN: How to write great PHP code?

3 points by hella ↗ HN
I've been able to hack together a few websites with PHP. But I still have no idea how to write good MVC, object-oriented code.

How/where can I learn this?

(Please don't say that I should pick another language. I'm fine with using PHP.)

7 comments

[ 242 ms ] story [ 1169 ms ] thread
Please help me to learn and grow. Oh, and don't tell me the best way to learn and grow because, well, because. You want advice but you already don't like the answer?
First, make sure you understand some of the higher level OO concepts of PHP like variable scope, inheritance, polymorphism, and visibility. You can easily do this in the php docs and stackoverflow, or other such Google searches.

Then take a look at Codeigniter, http://codeigniter.com/user_guide/ it's organized, explains the how and why of MVC, is very very well documented and has a lot of great libraries for getting started, then once you get familiar with how everything works you can begin to look under the hood and start figuring out why it works the way it does and continue learning from there.

Actually, if you really want to learn PHP then CodeIgniter isn't the best place to start IMO. It's great to use as a tool for production. It's probably okay to get going at the very beginning. But if you want to really learn PHP, especially the new additions for PHP 5.3 then I suggest looking the code and using a PHP 5.3 framework. Look for a really small one so that you can see everything in the framework without having to look through a ton of files. Fatfree is one good example. A CodeIgniter based PHP 5.3 framework is fuelphp.
I won't tell you to abandon php (I haven't entirely either for pragmatic reasons) but I would suggest "going to the source" and learning about smalltalk. Other than the meta object stuff it will, in the main, transfer over to how you write php and think about OO. More pragmatically I'd also recommend learning from and leveraging the SPL stuff (http://www.php.net/~helly/php/ext/spl/)
The http://php.net/oop page is a great start. It lists all "modern features" of PHP. Almost all of which are used to write "great code".