乐趣区

了解PHP容器Container的实现

看 Laravel 源代码了解 Container

i

<?php
function info($msg){echo $msg;}

class LogToFile {public function execute($message) {info('log the message to a 美羊羊 :'.$message);
    }
}

class UseLogger {
    protected $logger;

    public function __construct(LogToFile $logger) {$this->logger = $logger;}

    public function show() {
        $user = '小灰灰';
        $this->logger->execute($user);
    }
}

$useLogger = new UseLogger(new LogToFile());

$useLogger->show();

ii

A

<?php
 
function info($msg){echo $msg;}


class LogToFile {public function execute($message) {info('log the message to a 美羊羊  :'.$message);
    }
}

class LogToDD {public function execute($message) {info('log the message to 喜羊羊 :'.$message);
    }
}

 
class UseLogger {
    protected $logger;

    public function __construct(LogToFile $logger) {$this->logger = $logger;}

    public function show() {
        $user = '小灰灰';
        $this->logger->execute($user);
    }
}
$useLogger = new UseLogger(new LogToFile());
 
$useLogger->show();

B

<?php
 
function info($msg){echo $msg;}

class LogToFile {public function execute($message) {info('log the message to a 美羊羊  :'.$message);
    }
}

class LogToDD {public function execute($message) {info('log the message to 喜羊羊 :'.$message);
    }
}

 
class UseLogger {
    protected $logger;

    public function __construct(LogToDD $logger) {$this->logger = $logger;}

    public function show() {
        $user = '小灰灰';
        $this->logger->execute($user);
    }
}
$useLogger = new UseLogger(new LogToDD());
 
$useLogger->show();

iii

<?php

function info($msg){echo $msg;}

interface Logger {public function execute($message);
}

class LogToFile implements Logger {public function execute($message) {info('log the message to a 美羊羊 :'.$message);
    }
}

class LogToDD implements Logger {public function execute($message) {info('log the message to 喜羊羊 :'.$message);
    }
}
 
class UseLogger {
    protected $logger;

    public function __construct(Logger $logger) {$this->logger = $logger;}

    public function show() {
        $user = '小灰灰';
        $this->logger->execute($user);
    }
}

$useLogger = new UseLogger(new LogToFile());
 
$useLogger->show();

echo '<br>';

$useLogger = new UseLogger(new LogToDD());
 
$useLogger->show();

终极版

<?php
 
function info($msg){echo $msg;}

interface Logger {public function execute($message);
}

class LogToFile implements Logger {public function execute($message) {info('log the message to a 美羊羊 :'.$message);
    }
}

class LogToDD implements Logger {public function execute($message) {info('log the message to 喜羊羊 :'.$message);
    }
}
 
class UseLogger {
    protected $logger;

    public function __construct(Logger $logger) {$this->logger = $logger;}

    public function show() {
        $user = '小灰灰';
        $this->logger->execute($user);
 
    }
}

class SimpleContainer {

    // 用于存储所有绑定 key-value
    protected static $container = [];

    public static function bind($name, Callable $resolver) {static::$container[$name] = $resolver;
    }

    public static function make($name) {if(isset(static::$container[$name])){$resolver = static::$container[$name] ;
            return $resolver();}
        throw new Exception("Binding does not exist in container");
    }
}

SimpleContainer::bind(Logger::class, function () {return new LogToDD();
});

$useLogger3 = new UseLogger(SimpleContainer::make(Logger::class));


$useLogger3->show();
退出移动版