2

I'm trying to separate additional functional to other files, that can be included at runtime or no. I think it's a good way to build modular architecture, but I can't find ways to implement it without inheritance. The deal is that I have base class:

class Model {
    protected $name;
    function __construct() {
        $this->name = 'default';
    }
    function getName() {
        return 'Model: ' . $this->name;
    }
}

And specific class:

class Item extends Model {
    function getName() {
        return 'Item: ' . $this->name;
    }
}

That I use through the project:

$item = new Item();
echo $item->getName();

The result will be:

Item: default

I want to change getName() behaivor at other place to get result like this:

class ExtendedItem extends Item {
    function getName() {
        return parent::getName() . ' and this is extended method';
    }
}

$extendedItem = new ExtendedItem();
echo $extendedItem->getName();

Item: default and this is extended method

But I want to achieve it without using child class, i.e without touching Item or Model class and without changing objects initialization. I suppose there will be more then one extensions like this and they will included dynamically. In what way I should searching or can you explain how to achieve this?

Thanks in advance for answers.

UPD: In other words I want to change class method behavior outside class code preserving it's default functional.

  • 3
    [Traits](http://www.php.net/manual/en/language.oop5.traits.php) would be one option – Mark Baker Jun 17 '15 at 21:58
  • 1
    "there will be more then one extensions like this and they will included dynamically." This is not a case for either Traits or Inheritance, if you want to make an object add some text to the end of a function call, then give the object that text via the constructor, via a public property, via a `set()` method, or in the function call itself. – Sammitch Jun 17 '15 at 22:02
  • 1
    @MarkBaker I tried to achieve this with traits, but the Item class shouldn't know about extendings. – Danila Loginov Jun 17 '15 at 22:05
  • @Sammitch adding text to the end of a function call is just an example – Danila Loginov Jun 17 '15 at 22:07
  • 2
    Then you're probably looking for [Callbacks](http://php.net/manual/en/language.types.callable.php). You can't dynamically extend classes. At least not in a way that is either secure or maintainable. – Sammitch Jun 17 '15 at 22:41
  • @Sammitch sorry for a little long reply :D, just reviewed my old question. You were absolutely right, despite it is very sophisticated question, Callbacks fits the best among other approaches, thank you very much! – Danila Loginov Jan 11 '18 at 22:13
  • 1
    Taking a fresh look at the question/comments, you could use a [Decorator](https://sourcemaking.com/design_patterns/decorator/php) for a more compositional approach as well. – Sammitch Jan 11 '18 at 22:29
  • Yes, combining it with Callbacks specifying model sorting/filtering logic and other similar injections gives me what I want, thank you! – Danila Loginov Jan 14 '18 at 14:13

0 Answers0