Hinting "object"?

You can hint “array” or a specific class, is there any way to hint that any object can be used? Essentially I’m using reflection in order to work out what methods are available. Obviously reflection only works on objects so it would be nice to be able to type hint like this:


function foo(object $bar) {
}

And while this code parses even without “object” being defined (I guess it’s defined internally by php). As soon as you attempt to pass an object to the function, it errors because it’s not of type “object”.

Any ideas if this is possible? The only other alternative I can think of is:


function foo($bar) {
if (!is_object($bar)) throw new Exception('$bar must be of type Object');
}

Thanks!

I believe your second approach would be the preferred approach. It is my understanding that type hinting in PHP is really a way to apply constraints to your functions, meaning the object passed to that function MUST match the hinted type.

So in your first example, you would have to cast your regular object to (object), while passing it into your function.

function foo(object $bar) { 
}  
$example = new Example();
foo((object)$example);

Casting is considered an expensive process in most languages, so it is usually best to avoid it if possible.

You would have to do something like your second piece of code.

For your first way, these would work for example:

function foo( Exception $bar ) {}
function foo( stdClass $bar ) {}
function foo( myDefinedClass $bar ) {}

In those examples $bar must be an instance of the type. So if the object you’re passing in must be an instance (or decendant) of a particular class, you can use that. Otherwise, do an if statement inside as you posted.

Interesting, but doesn’t casting it to (object) remove its behaviour? I want to keep it as is so I can use reflection to see what methods are available on it. Even if it doesn’t, casting is ugly, and forcing casting each time the function is called is messy! The is_object() method is tidier I think!

My question was that I wanted to be able to pass any kind of object, but any object, just not an array or primitive.

class Bar {}

function foo( stdClass $bar ) {
return 1;
}

$b = new Bar;

echo foo($b);

That only works if I use up the chance of extending, and would be terribly restrictive.


Bar extends stdClass {}

… that surprised me a bit, because I understood that in some languages all classes are derived from a basic class (Java?).

I wonder if all PHP classes maybe implement a shared interface?

No. That’s why you can’t do what the OP is attempting. And the first reply is not right. You just can’t type hint for an object unless you know the type of object it is. “object” or “stdClass” or anything is not a base which all PHP objects extend; there is no such base.

The first reply was right at the beginning (emphasis added to show the initial response was spot on), the “first” approach may be flawed, but I never recommended that he perform the first approach.

I believe your second approach would be the preferred approach. It is my understanding that type hinting in PHP is really a way to apply constraints to your functions, meaning the object passed to that function MUST match the hinted type.

The second part of my response was simply from my .NET knowledge where such a technique could be utilized (didn’t realize PHP didn’t permit this capability).