Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

My experience has primarily been in Java, but I'm working on a PHP project and conscious of my lack of knowledge of idiomatic PHP. What resources (online or print) will help me avoid making the mistakes that people with Java goggles normally make? First prize would be something that would give me short in-depth examinations of important PHP ideas, a la Joshua Bloch's Effective Java.

share|improve this question
    
    
I doubt that there actually exists idiomatic PHP. –  Philip Apr 17 '11 at 3:15
    
Would you care to elaborate? Surely some ideas about "good" and "bad" PHP usage must have surfaced? –  Fritz Meissner Apr 17 '11 at 8:58
add comment

1 Answer

up vote 3 down vote accepted

As a professional PHP programmer (with a CS degree and exposure to idiomatic Java and idiomatic Ruby amongst other things) I've found that PHP idioms really depend on the framework (or lack thereof) that you're using.

It's quite possible - and, in my personal opinion, very sensible - to write PHP in a Java-ish style. Many of the best frameworks (Zend, Doctrine, PHPUnit, Facebook's various bits and bobs) will feel comfortable if you do so. Although PHP is weakly typed, making use of the strong typing features will enable you to make the most server utiliies such as Facebook's hiPHoP compiler.

I have come across many idioms in PHP, however, many of which are project dependent. Anything with it's roots in PHP4 will probably feel more like C or Perl, with extensive use of includes, structs (i.e objects where all props are public) and global functions.

Many of the newer features in PHP5.3 (such as closures) are designed to make porting code from languages such as Ruby easier, although it will not feel idiomatic if you do so as it makes for ugly PHP.

When using PHP as a tempting language - anyone who's seen a Wordpress theme will know what I mean - will find a different set of idioms comes into play. Even so, I've never yet come across a PHP templating idiom that felt as natural as Rails RHTML or Java's XML based stuff - except for Facebook's XHP language extension, and that's not widely supported anywhere else.

Having said all that, here are my prime language tips:

  • PHP's dynamic error model is very alien to the clean exceptions model in Java. Specifically, while a NullPointer exception is easy to handle in Java, in PHP it'll kill you script dead regularly. It's possible to convert MOST error types into Exceptions with a little trickery (although this is not the default behaviour) - but not "method accessed on a non-object"!!

  • PHP's dynamic typing and casting between strings and integers can take a little getting used to. Remember that false == 0 == "". Often you should compare for IDENTITY (not just equality) with false - remember the === (triple equals) operator.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.