PHP Type Hinting: array supported, object NOT?

Posted by Marius Burz on Stack Overflow See other posts from Stack Overflow or by Marius Burz
Published on 2009-10-10T11:19:00Z Indexed on 2010/04/17 21:33 UTC
Read the original article Hit count: 233

Filed under:
|

Am I missing something or there really is no support for generic object type hinting in PHP 5.x?

I find it really strange that hinting arrays is supported while hinting objects is not, at least not out of the box.

I'd like to have something like this:

function foo(object $o)

Just as we have:

function foo(array $o)

Example of possible use: methods of an objects collection class.

Workaround: using an interface "Object" implemented by all classes or extending all classes from a generic class "Object" and writing something like this:

function foo(Object $o)

Well, that just ain't cute.

Edit: somebody suggested in a deleted post using stdClass. It doesn't work:

Catchable fatal error: Argument 1 passed to c::add() must be an instance of stdClass, instance of b given

© Stack Overflow or respective owner

Related posts about php5

Related posts about oop