php - Type hinting for any object
Get the solution ↓↓↓I've been working on code that's intended to be used with objects, without really caring what the kind of object is. I wanted to type hint that the method being written expected an object of any type, but ran into some difficulty.
I triedfunction myFunc (object $obj)
andfunction myFunc (stdClass $obj)
but both of these generated errors when I tried to pass objects in:
Catchable fatal error: Argument 1 passed to MyClass::MyFunc() must be an instance of object, instance of ObjectActualClass given
The same happened withstdClass
as well
What am I missing? I thought that all classes that didn't explicitly inherit from another class inherited fromstdClass
, meaning that the base class of every class in PHP would bestdClass
. Is this not the case?
Answer
Solution:
stdClass is NOT a base class! PHP classes do not automatically inherit from any class. All classes are standalone, unless they explicitly extend another class. PHP differs from many object-oriented languages in this respect.
Answer
Solution:
The best way to enforce this would be to create a degenerate interface calledObject
. A degenerate interface means it has no defined methods.
interface Object {
// leave blank
}
Then in your base classes, you can implementObject
.
class SomeBase implements Object {
// your implementation
}
You can now call your function as you wanted to
function myFunc (Object $obj);
myFunc($someBase);
If you pass any object which inherits from yourObject
interface, this type hint will pass. If you pass in an array, int, string etc, the type hint will fail.
Answer
Solution:
Well it only took eight years, but this will soon be possible: PHP 7.2 introduces theobject
type hint! As I write this, it's currently in the RFC stage, and is due to be released in November.
Update, 30th November: PHP 7.2 has been released
This behaves exactly as you might expect:
<?php
class Foo {}
class Bar {}
function takeObject(object $obj) {
var_dump(get_class($obj));
}
takeObject(new Foo);
takeObject(new Bar);
takeObject('not an object');
Will result in:
string(3) "Foo"
string(3) "Bar"
Fatal error: Uncaught TypeError: Argument 1 passed to takeObject() must be an object, string given, called in...
One side-effect of this is that , which unfortunately renders @Gaz_Edge's existing solution above broken. Fortunately, all you have to do to fix it is delete the interface.
Answer
Solution:
Although there is no type hinting for objects, you can use:
if (!is_object($arg)) {
return;
}
Answer
Solution:
There is no base class that all objects extend from. You should just remove the typehint and document the expected type in the@param
annotation.
Answer
Solution:
There is no built-in mechanism to do this without requiring all users of your interface to extend a specified class. But why would you want to do this anyway? What do all object types have in common that's enough to make them suitable input for your API?
In all probability you wouldn't gain anything even if able to type hint like this. On the other hand, type hinting a parameter to implement an interface (such asTraversable
) would be much more meaningful.
If you still want something akin to type hinting, the best you can do is substitute a runtime check withis_object
on the parameter.
Answer
Solution:
As of php 7.2 this feature has now been implemented. you can type hint for any object now.
function myFunc(object $myObject): object {
return $myObject;
}
You can review this in the official documentation
Answer
Solution:
Typehint forstdClass
works since PHP 5.3+ (if I am not wrong).
Following is valid code using typehint forstdClass
construct:
Exampletest.php
:
class Test{
function hello(stdClass $o){
echo $o->name;
}
}
class Arg2 extends stdClass{
public $name = 'John';
function sayHello(){
echo 'Hello world!';
}
}
$Arg1 = new stdClass();
$Arg1->name = 'Peter';
$Arg2 = new Arg2();
$Arg2->sayHello();
$test = new Test();
// OK
$test->hello($Arg1);
$test->hello($Arg2);
// fails
$test->hello(1);
Prints out:
Hello world!
Peter
JohnCatchable fatal error: Argument 1 passed to Test::hello() must be an instance of stdClass, integer given, called in test.php on line 32 and defined in test.php on line 5
Answer
Solution:
You could do something like this:
function myFunc ($obj)
{
if ($obj instanceof stdClass) { .... }
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: method illuminate\database\eloquent\collection::paginate does not exist.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.