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

closed as off-topic by MichaelT, Ampt, jwenting, gnat, Bart van Ingen Schenau Jun 5 at 8:06

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions asking us to recommend a tool, library or favorite off-site resource are off-topic for Programmers as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it." – MichaelT, Ampt, jwenting, gnat, Bart van Ingen Schenau
If this question can be reworded to fit the rules in the help center, please edit the 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
    
phptherightway.com –  lortabac Jun 4 at 9:44
add comment

1 Answer 1

up vote 4 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 of server utilities such as Facebook's HiPHoP compiler.

I have come across many idioms in PHP, however, some of which are project dependent. Anything with its 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.

Some 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, 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 - you 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 your 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

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