Why are PHP function signatures so inconsistent?
- by Shamim Hafiz
I was going through some PHP functions and I could not help notice the following:
<?php
function foo(&$var) { }
foo($a); // $a is "created" and assigned to null
$b = array();
foo($b['b']);
var_dump(array_key_exists('b', $b)); // bool(true)
$c = new StdClass;
foo($c->d);
var_dump(property_exists($c, 'd')); // bool(true)
?>
Notice the array_key_exists() and property_exists() function.
In the first one, the property name(key for an array) is the first parameter while in the second one it is the second parameter. By intuition, one would expect them to have similar signature. This can lead to confusion and the development time may be wasted by making corrections of this type.
Shouldn't PHP, or any language for that matter, consider making the signatures of related functions consistent?