I’m having weird problems with PHP OOP and type hinting. Here’s 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 won’t run, saying that method is not compatible with the interface declaration. I would think it is compatible since C extends AC – do I miss something? How am I expected to implement this sort of functionality?
Imagine you have a class
Bwhich also extendsAC. ThenIrequires that any of its implementations also acceptBas argument to method. YourInterfaceImplementation, however, doesn’t.The bigger picture: You might want to reconsider your design if you need to specify a concrete type in the implementation. The idea is that to the outside world, all that needs to be known is encoded by
ACand there should not be anInterfaceImplementationthat ever needs to know which concrete subclass is being transferred. Maybe the specific stuff can be embedded inC‘s code and generically called through a method exposed byAC?Yet another update: You might be able to achieve what you want using generics, but I don’t think they exist in PHP. I still think if you share the details of the design problem that might make another interesting question 🙂