Polymorphism

interface Text
{
    public function getText();
}

class BigText implements Text
{
    public function getText()
    {
        echo 'BIG';
    }
}

class LittleText implements Text
{
    public function getText()
    {
        echo 'мелкий';
    }
}

class NewsPaper
{
    /**
     * @var Text
     */
    private $text;

    /**
     * NewsPaper constructor.
     * @param Text $text
     */
    public function __construct(Text $text)
    {
        $this->text = $text;
    }

    public function setText($text)
    {
        $this->text = $text;

        return $this;
    }

    public function getText()
    {
        return $this->text;
    }
}

$array = [
    new BigText(),
    new LittleText()
];

foreach ($array as $value) {
    $newsPaper = new NewsPaper($value);
    echo $newsPaper->getText()->getText();

}
exit();

Комментариев нет:

Отправить комментарий

JavaScript learn

Чтобы вставить элемент после какого-то элемента, нужно создать прототип. Element.prototype.appendAfter = function (element) { element.paren...