tags:

views:

353

answers:

6

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

+2  A: 

Since type hinting should make the client code adapt to your API, your solution with accepting interfaces seems just about right.

Look at it this way: yourMethod(array $input) gives yourMethod() an array to use, thereby you know exactly which native functions that applies and can be used by yourMethod().

If you specify your method like: yourSecondMethod(yourInterface $input) you'd also know which methods that can be applied to $input since you know about/can lookup which set of rules that accompanies the interface yourInterface.

In your case, accepting any object seems wrong, because you don't have any way of knowing which methods to use on the input. Example:

function foo(Object $o) {
    return $o->thisMethodMayOrMayNotExist();
}

(Not implying that syntax is valid)

chelmertz
That's not the case for an objects collection class which will be acting as a (pseudo) container that won't be invoking any methods on its children nor will it use any of their attributes / properties.Knowing a variable is an object also makes it possible to use lots of native functions on it, so I don't really see your point.
Marius Burz
How about using a type hint such as SplObjectStorage or ArrayObject?
chelmertz
A: 

No, it can't be done. I wasn't missing anything.

Marius Burz
A: 

Why would you want to hint object when you can hint an actual class name instead - this would be much more useful. Also remember that you can't hint int,float, bool, string or resource either.

too much php
Why would you want to hit an array? Remember all those other types you can't hint... Since there is no common base class from which all PHP classes descend, it makes sense to be able to hint an object, just as it makes to hint an array. My opinion, but I doubt I'm alone on this one.
Marius Burz
A: 

I feel your pain, but I can't find a way of doing it either.

Despite what a number of other posters have said, it makes perfect sense to want 'Object' type hinting; they just haven't considered a scenario that requires it.

I am trying to do some work with the reflection API, and because of that I don't care what class is passed to my function. All I care is that it's an object. I don't want an int, a float, a string or an array. I want an object. Given that reflection is now part of PHP, it definitely makes sense to have object type hinting.

A: 

Here's another example where it is required...

I've created a class to implement record locking. Records being one of a number of different object types. The locking class has several methods which require an object (the one to be locked) but don't care what type of object it is.

E.g.

public static function lockRecord($record, User $user, $timeout=null)
{
    if(!is_object($record)) throw new \InvalidException("Argument 1 must be an object.");

    $lock=new Lock();
    $lock->setRecord($record);
    $lock->setUser($user);
    $lock->setTimeout($timeout);
    $lock->activate();
    return($lock);
}

You'll see that my solution was to use is_object() and throw an exception, but I'd far rather be able to do it with type hinting instead.

Ok, so not the end of the world, but I think it's a shame.

Matt Ralston
A: 

You cannot just say "object" when type casting an object... you must define WHICH object you are expecting.

From: http://php.net/manual/en/language.oop5.typehinting.php

class MyClass

{ /** * A test function * * First parameter must be an object of type OtherClass */ public function test(OtherClass $otherclass) { echo $otherclass->var; }

/**
 * Another test function
 *
 * First parameter must be an array
 */
public function test_array(array $input_array) {
    print_r($input_array);
}

}

// Another example class class OtherClass { public $var = 'Hello World'; }

Bhol
Sorry, paste of code failed, see the link for a better sample.
Bhol