News for hackers. Stuff that matters.

Ask HN: How to write great PHP code?

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 | 3 points | on HN )
Re:Ask HN: How to write great PHP code? by mopoke on Monday January 03, 2011 @09:47PM (#2065646)
I'd suggest choosing one of the many MVC frameworks for PHP (http://www.phpframeworks.com/) and reading that framework's tutorials and guides then getting to work on a real project. For example, there's a series of tutorials for Yii (http://www.larryullman.com/series/learning-the-yii-framework...) as well as an intro to the concepts behind the MVC pattern (http://www.larryullman.com/2009/10/08/understanding-mvc/)
Re:Ask HN: How to write great PHP code? by tgriesser on Monday January 03, 2011 @10:20PM (#2065701)
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.

Re:Ask HN: How to write great PHP code? by gexla on Monday January 03, 2011 @10:28PM (#2065719)
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.
Re:Ask HN: How to write great PHP code? by shaunxcode on Tuesday January 04, 2011 @01:08AM (#2066049)
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/)
Re:Ask HN: How to write great PHP code? by lastkarrde on Tuesday January 04, 2011 @04:10AM (#2066388)
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".
Re:Ask HN: How to write great PHP code? by daz_miller on Tuesday January 04, 2011 @12:40AM (#2065995)
take a look at the Zend Framework and site. Some amazing docs and code there. http://framework.zend.com/
Re:Ask HN: How to write great PHP code? by dwc on Monday January 03, 2011 @09:52PM (#2065656)
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?