We can restrict type of method parameters; for example, we should say that function parameter should be an instance of object described in class with name “Some Class”.
function some_function(Some_Class $object) {
}
Is there any php native posibilities to restrict method parameter with two or more classes? For examle, “Some Class” or “Some Class2” or “Some Class3”.
Or maybe there is any way to restrict method parameter with classes which implements interface with name “Some_Interface”?
Thank you.
You can do it with an
interface, e.g.Some classes implementing the interface
Then you can use it as a TypeHint like this:
This way you make sure the param passed to
some_function()has a methodlog(). It doesn’t matter which concrete you pass to it (FileLog,DbLogorSysLog), but just that these classes implement the interface.