autoload.php<?phpspl_autoload_register(function ($className)){ $path = str_replace('\\','/',$className); require dirname(__DIR__).DIRECTORY_SEPARATOR.$path.'.php';});Travel1.php<?php//旅游namespace base;//设置引用的外部类名的别名use base\inc1\Car;use base\inc1\Train;use base\inc1\Plane;require __DIR__.'/autoload.php';class Travel1{ //交通工具 private $vehicle; //构造方法 public function __construct($vehicle) { switch (strtolower($vehicle)) { case 'car'; $this->vehicle=new Car(); break; case 'train'; $this->vehicle=new Train(); break; case 'plane'; $this->vehicle=new Plane(); break; } } //调用外部一个依赖对象 public function travelModel() { return $this->vehicle->drive().':去旅行'; }}//客户端调用echo (new Travel1('car'))->travelModel(),'<br>';echo (new Travel1('train'))->travelModel(),'<br>';echo (new Travel1('plane'))->travelModel(),'<br>';Travel2.php<?php//旅游namespace base;//设置引用的外部类名的别名use base\inc1\Car;use base\inc1\Train;use base\inc1\Plane;require __DIR__.'/autoload.php';//工厂类,专用于创建实例class Factory{ protected static $instance=null; public static function getInstance($vehicle) { switch (strtolower($vehicle)) { case 'car': self::$instance=new Car(); case 'train': self::$instance=new Train(); case 'plane': self::$instance=new Plane(); } //返回当前具体的交通工具 return self::$instance; }}class Travel2{ //交通工具 private $vehicle; //构造方法 public function __construct($vehicle) { $this->vehicle=Factory::getInstance($vehicle); } //调用外部一个依赖对象 public function travelModel() { return $this->vehicle->drive().':#####去旅行'; }}//客户端调用echo (new Travel2('car'))->travelModel(),'<br>';echo (new Travel2('train'))->travelModel(),'<br>';echo (new Travel2('plane'))->travelModel(),'<br>';Travel3.php<?php//旅游namespace base;//设置引用的外部类名的别名use base\inc2\Car;use base\inc2\Train;use base\inc2\Plane;use base\inc2\Ship;use base\inc2\iVehicle;require __DIR__.'/autoload.php';class Travel3{ //交通工具 private $vehicle; //构造方2 public function __construct(iVehicle $vehicle) { $this->vehicle=$vehicle; } //调用外部一个依赖对象 public function travelModel() { return $this->vehicle->drive().':#####去旅行'; }}//客户端调用$car =new Car();echo (new Travel3($car))->travelModel(),'<br>';echo (new Travel3(new Train()))->travelModel(),'<br>';echo (new Travel3(new Plane()))->travelModel(),'<br>';echo (new Travel3(new Ship()))->travelModel(),'<br>';inc1/Car.php<?phpnamespace base\inc1;class Car{ public function drive() { return '开汽车'; }}inc1/Plane.php<?phpnamespace base\inc1;class Plane{ public function drive() { return '乘飞机'; }}inc1/Train.php<?phpnamespace base\inc1;class Train{ public function drive() { return '坐火车'; }}inc2/Car.php<?phpnamespace base\inc2;class Car implements iVehicle{ public function drive() { return '开汽车'; }}inc2/iVehicle.php<?phpnamespace base\inc2;//交通工具的接口interface iVehicle{ public function drive();}inc2/Plane.php<?phpnamespace base\inc2;class Plane implements iVehicle{ public function drive() { return '乘飞机'; }}inc2/Ship.php<?phpnamespace base\inc2;class Ship implements iVehicle{ public function drive() { return '坐轮船'; }}inc2/Train.php<?phpnamespace base\inc2;class Train implements iVehicle{ public function drive() { return '坐火车'; }}
lesson1
##Container.php<?php//容器类namespace _1205;//引入闭包别名use Closure;class Container{ //容器数组/实例容器/类实例容器 protected $instance = []; //将类实例化的过程绑定到容器中 public function bind($alias,Closure $process) { $this->instance[$alias]=$process;// //创建产品实例// $product = new Product();// //创建制造商实例// $maker = new Marker();// //制造商注入到产品类中// return $product->get($maker); } //取出保存在容器中的实例化过程的闭包,并执行他 public function make($alias) { return $this->instance[$alias]();// call_user_func_array() }}##Mark.php<?phpnamespace _1205;//制造商class Marker{ public function get() { return '华为'; }}##Product.php<?phpnamespace _1205;//商品类class Product{ public function get(Marker $marker) { return '该手机是由:'. $marker->get().'生产的'; }}##demo1.php<?phpnamespace _1205;require 'Product.php';require 'Marker.php';//先不用容器class Client1{ //输出商品与制造商 public function show() { //创建产品实例 $product = new Product(); //创建制造商实例 $maker = new Marker(); //制造商注入到产品类中 return $product->get($maker); //相当于$instances[obj]=new Product(); //make()方法去除new Product(); }}//客户端调用echo (new Client1())->show();##demo2.php<?phpnamespace _1205;require 'Product.php';require 'Marker.php';require 'Container.php';//先不用容器class Client2{ //输出商品与制造商 public function show(Product $product,Marker $marker) {// //创建产品实例// $product = new Product();// //创建制造商实例// $maker = new Marker();// //制造商注入到产品类中 return $product->get($maker); //相当于$instances[obj]=new Product(); //make()方法去除new Product(); }}//客户端调用//将类实例绑定到容器中并实例化且返回$container = new Container();//将产品类与它的实例化代码绑定到容器中$container->bind('product',function (){return new Product();});$container->bind('maker',function (){return new Maker();});//创建实例并返回$product = $container->make('product');$maker = $container->make('maker');echo (new Client2())->show($product,$maker);
总结:如果只学到这里的话,看懂很容易,但是自己写基本上是不可能的了,这辈子都不可能的了,只能希望在后面学习中看看能不能有突破了。
Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:可以先理解这里面的原理, 写不出来, 并不会妨碍后面的框架学习的.... , 况且, 你在工作中, 也不会去真的手写这些底层代码, 框架全部都为你准备好了, 直接调用就行...
当然, 自己能写出来更好, 写不出来就记住流程和原理吧
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!