I have a class 'Collection' that has an add method. The add method should only accept objects. So this is the desired behavior:
$x=5;
$obj=new Foo;
$collection=new Collection;
$collection->add($obj);
$collection->add($x);
According to the PHP manual, you can introduce typehint methods, previously using the $argclass name. Since all PHP classes are children stdClass, I realized that this method signature would work:
public function add(stdClass $obj);
But it fails: "The argument must be an instance of stdClass."
If I change the signature to the parent class defined by me, then it works:
class Collection {
public function add(Base $obj){
}
}
$collection->add($foo);
Does anyone know how to enter a hint for a shared object?
source
share