Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type hinting for any object

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 tried function myFunc (object $obj) and function 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 with stdClass as well

What am I missing? I thought that all classes that didn't explicitly inherit from another class inherited from stdClass, meaning that the base class of every class in PHP would be stdClass. Is this not the case?

like image 562
GordonM Avatar asked Oct 20 '11 16:10

GordonM


People also ask

Do type hints do anything?

Type hints improve IDEs and linters. They make it much easier to statically reason about your code. Type hints help you build and maintain a cleaner architecture. The act of writing type hints forces you to think about the types in your program.

How do you write hint objects in Python?

In a type hint, if we specify a type (class), then we mark the variable as containing an instance of that type. To specify that a variable instead contains a type, we need to use type[Cls] (or the old syntax typing. Type ). We need to add type hints for make_animal() .

Which data types can be declared with Type hinting?

With Type hinting we can specify the expected data type (arrays, objects, interface, etc.) for an argument in a function declaration. This practice can be most advantageous because it results in better code organization and improved error messages.

What is Type hinting Python?

Introduction to Python type hints It means that you need to declare types of variables, parameters, and return values of a function upfront. The predefined types allow the compilers to check the code before compiling and running the program.


2 Answers

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.

like image 133
Abdullah Avatar answered Sep 24 '22 23:09

Abdullah


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.

like image 43
GWed Avatar answered Sep 24 '22 23:09

GWed