So I'm working on a server/client system and trying to learn some OOP.
I have set up the following interfaces:
interface Host {}
interface Server extends Host {}
interface Client extends Host {}
interface Message {
public function __construct( Host $x, ... );
}
interface Request extends Message {}
interface Response extends Message {}
This works just fine. No error. I wanted to extends the interfaces in the way that I have, because the client and server will share A LOT of methods, and the request and response will share A LOT of methods. Design-wise, this seems to make sense to me.
But, the Request should only be allowed to instantiate with a Client, and a Response should only be allowed to instantiate with a Server. Something like this:
interface Message {
public function __construct( Host $x, ... );
}
interface Request extends Message {
public function __construct( Client $x, ... );
}
interface Response extends Message {
public function __construct( Server $x, ... );
}
But this doesn't work, because the Message/Request/Response constructors have different signatures. It seems to me that, logically, they are the same, since anything you pass to "new Request()" or "new Response()" would definitely successfully pass to "new Message()", since Client and Server are children of Host, and therefore inherit its public and protected properties and methods.
Nevertheless, it doesn't work, as I'd hoped it would. I have been thinking about this problem for a long time now, and tried various solutions. But, all to no avail. I just keep going in circles.
Can someone explain to me what I'm missing, why this logically SHOULDN'T work, or, if I can make it work somehow? I would appreciate any and all input!! 🙂 Thanks!