Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
31
Help us understand the problem. What is going on with this article?
@rana_kualu

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

More than 5 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{}とか書いとけば、まあだいたい同じ意味になるだろう。

31
Help us understand the problem. What is going on with this article?
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
rana_kualu
不労所得で生きたい。

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
31
Help us understand the problem. What is going on with this article?