What surprises have other people found with writing PHP web applications? There's the well known and to be fixed issue with compile time class inheritance but I know of a couple others and wanted to try and build a list of the top gotcha's of the language.
Note:
I've held several positions as a Sr. PHP5 developer so PHP work pays my bills, this question is not meant to bust on PHP as a language as every single language I've worked with has some well known or not so well known surprises.
I'm not sure if this counts, but the need to compile PHP scripts is a huge performance issue. In any serious PHP project you need some kind of compiler cache like APC, eAccelerator, PHP Accelerator, or the (commercial) Zend Platform.
Recursive references leak memory
If you create two objects and store them inside properties of each other, the garbage collector will never touch them:
$a = new stdClass;
$b = new stdClass;
$a->b = $b;
$b->a = $a;
This is actually quite easy to do when a large class creates a small helper object which usually stores the main class:
// GC will never clean up any instance of Big.
class Big {
function __construct() {
$this->helper = new LittleHelper($this);
}
}
class LittleHelper {
function __construct(Big $big) {
$this->big = $big;
}
}
As long as PHP is targeted at short fast page requests, they are not likely to fix this issue. This means that PHP can't be depended on for daemons or other applications that have a long lifespan.
require_once and include_once can often result in major performance killers when used excessively. If your including/require a file that holds a class... a pattern like so can save some serious processing time.
class_exists("myFoo") or require("myFoo.someClass.php");
Update: This is still a issue - http://www.techyouruniverse.com/software/php-performance-tip-require-versus-require_once
Update: Read the selected answer for the following question: Would performance suffer using autoload in php and searching for the class file? If implemented along these lines, you pretty much minimize as best as possible the penalties for file include/requires.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With