PHP OOP "Implementation Must Be Compatible"

I am having strange problems with PHP OOP and hint type. Here is an example:

abstract class AC {}

class C extends AC {}

interface I {
    function method(AC $abstract);
}

class InterfaceImplementation implements I {
    function method(C $concrete) {}
}

This code will not work, saying that it is methodincompatible with the declaration of the interface. I think it is compatible, since C extends AC - am I missing something? How do I expect to implement such functionality?

+5
source share
2 answers

Imagine you have a class Bthat also extends AC. Then it Irequires that any implementation of it also takes Bas an argument to the method. However, yours InterfaceImplementationdoes not.

. , , . , , , AC, InterfaceImplementation, - , . , C , AC?

. , , , , PHP. , , :)

+4

:

class InterfaceImplementation implements I {
    function method(AC $concrete) {}
}

C, .. ->method(new C());.

PHP :

, , , . .

+1

All Articles