Type hinting for any object Type hinting for any object php php

Type hinting for any object


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.


The best way to enforce this would be to create a degenerate interface called Object. A degenerate interface means it has no defined methods.

interface Object {   // leave blank}

Then in your base classes, you can implement Object.

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 your Object interface, this type hint will pass. If you pass in an array, int, string etc, the type hint will fail.


Well it only took eight years, but this will soon be possible: PHP 7.2 introduces the object 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

RFC: Object typehint

Discussion

This behaves exactly as you might expect:

<?phpclass 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...

See https://3v4l.org/Svuij

One side-effect of this is that object is now a reserved word, which unfortunately renders @Gaz_Edge's existing solution above broken. Fortunately, all you have to do to fix it is delete the interface.