PHP: how to reuse code (oop)?

I studied in php oop and is equipped with the concept of reusable code.

I saw an example like

interface iTemplate
{
    public function setVariable($name, $var);
    public function getHtml($template);
}
And implement it:

// Implement the interface
class Template implements iTemplate
{
    private $vars = array();

    public function setVariable($name, $var)
    {
        $this->vars[$name] = $var;
    }

    public function getHtml($template)
    {
        foreach($this->vars as $name => $value) {
            $template = str_replace('{' . $name . '}', $value, $template);
        }

        return $template;
    }
} 

I can understand the code, but I don’t know why it can be reused. Every time I want to add a new function to the iTemplate interface, I also need to change the Template class. I do not understand the concept of "reuse". I appreciate any help. Thank you

+3
source share
5 answers

Interfaces are not intended for code reuse. They are intended for abstraction. They allow classes using the template to test the interface instead of the base class of the template. In this way, it separates the implementation from the interface declaration.

, - template, template . , , , iTemplate ( ).

public function foo(Template $template) {

public function foo(iTemplate $template) {

, . . . :

, . , :

interface iBird {
    public function fly();
    public function speak();
    public function swim();
    public function walk();
}

class Duck implements iBird {
    public function fly() {
        //Fly here
    }
    public function speak() {
        // Quack here
    }
    public function swim() {
        //Swim here
    }
    public function walk() {
        //Walk here
    }
}

class Turkey implements iBird {
    public function fly() {
        //Fly here, but limited
    }
    public function speak() {
        //Make turkey sound here
    }
    public function swim() {
        throw new Exception('Turkeys can not swim!');
    }
    public function walk() {
        //Walk here
    }
}  

, , , walk(), , (, , DRY)...

, :

abstract class Bird implements iBird {
    public function fly() {
        //Fly here
    }
    abstract public function speak();
    public function swim() {
        //Swim here
    }
    public function walk() {
        //Walk here
    }
}

class Duck extends Bird {
    public function speak() {
        //Quack here
    }
} 

class Turkey extends Bird {
    public function speak() {
        //Make turkey sound here
    }
    public function swim() {
        throw new Exception('Turkeys can not swim!');
    }
}  

, 3 ! speak(), ( ).

? , , . , ... , , NonSwimmingBird, Bird, . a NonFlyingBird ShortRangeBird...

, . , , . ? , . . , ? ... , " ". ( , , , . . , )...

, . 2 "" , , . , , , , ( , ). , DRY , . DRY, , , , . , ...

+6

1 . , . - . : setVariable . PHP : __get() __set().

0

, , - . , , . iPlugin:

interface iPlugin {
   public function init();
   /* .. */
}

. , init().

0

OO , .

, , , .

, , , , .

PHP - , , .

, "" , , . , "inline", , . , .

, , , X, Y, .

. , , , , , , , , , .

0

- - , .

0

Source: https://habr.com/ru/post/1764537/


All Articles