Help us understand the problem. What is going on with this article?

DI(依存性注入)がいみわからん

More than 3 years have passed since last update.
interface ILogger{
    /**
     * @param String テキスト
     */
    public function show(string $string);
}

class BrowserLogger implements ILogger{
    /**
     * @Override
     */
    public function show(string $string){
        print($string . '<br />');
    }
}
class ErrorlogLogger implements ILogger{
    /**
     * @Override
     */
    public function show(string $string){
        error_log($string);
    }
}

class HelloWorld{
    private $logger;
    /**
     * @param Logger
     */
    public function __construct(ILogger $logger){
        $this->logger = $logger;
    }

    /**
     * @param String $string
     */
    public function show(string $string){
        $this->logger->show($string);
    }
}

$helloWorld1 = new HelloWorld(new BrowserLogger());
$helloWorld1->show('hello, world');

$helloWorld2 = new HelloWorld(new ErrorlogLogger());
$helloWorld2->show('hello, world');

最低限の概念だけ実装したらこんなかんじだと思うんだけど、
で、だからこれがどうしたんだ、という気がしてならない。
だってみんな普通public function setPdo(PDO $pdo){ $this->pdo=$pdo; }とかやるよね?
その程度ことをわざわざDIとか言ってるのか?
PDOはInterfaceじゃないけど、かわりにabstract class PDOInterface extends PDO{}とか書いとけば、まあだいたい同じ意味になるだろう。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした