malheirosrafa
malheirosrafa

Reputation: 13

How to overload child class methods in PHP, respecting OOP and SOLID principles, when method signature must change?

How to overload child classes methods in PHP respecting OOP and SOLID principles, when method signature must change. There's some design pattern intended to this?

<?php

class Client extends People
{
    protected function fillData(ClientDTO $clientDTO)
    {
        parent::fillData($clientDTO);
        ...
    }
}

class People
{
    protected function fillData(PeopleDTO $peopleDTO)
    {
        ...
    }
}

class ClientDTO extends PeopleDTO
{
   protected $orders;
}

class PeopleDTO
{
   protected $name;
}

?>

Upvotes: 0

Views: 104

Answers (1)

m0onspell
m0onspell

Reputation: 545

I wouldn't worry here about changing a method signature, because signatures are compatible in your particular case as long as ClientDTO is a subclass of PeopleDTO. You can pass ClientDTO instance to People::fillData and it will still work. So I would say, no worries from that point of view. But you should use interfaces in place of hardcoded class dependencies in your signatures. You can call it a Strategy pattern if you want. This way, you'll respect Open\Closed and Dependency Inversion principles, since your classes will depend on abstractions. Or at least you should use some base abstract class if you think that creating interfaces would be an overkill, or your entities are very domain-specific.

P.S. I assume that you've made a mistake in your code sample: class ClientDTO should be class PeopleDTO

Upvotes: 1

Related Questions