
Originally Posted by
kyberfabrikken
And thus preventing a programmer from passing an
ArrayAccess ?
Well actually yes. In many cases the simplicity that comes with this kind of typing overcomes the flexibility of allowing developer to pass an object that implements ArrayAccess and/or Iterator and maybe even some other interfaces as well.
PHP Code:
class A {
/**
* @param $array - simple array
*/
public function foo(array $array) { }
}
instead of:
PHP Code:
class A {
/**
* @param mixed $array -- simple array or an object that implements ArrayAccess and Iterator interfaces
*/
public function foo($array) {
$interfaces = class_implements($array);
if( ! is_array($array) ||
! in_array('ArrayAccess', $interfaces)
! in_array('Iterator', $interfaces)
)
throw new Exception("first argument should be an array or it should implement ArrayAccess and Iterator interfaces.");
}
}
This becomes just stupid if I want EVERY method that takes an array as parameter to support objects that implement these special interfaces.
Bookmarks