1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
<?php class Test { public function __construct($param) { } public static function create($param) { return new static($param); } public static function createSelf($param) { return new self($param); } public static function checkStatic($obj) { if ($obj instanceof static) { echo get_class($obj) . " is static<br>"; } } public static function checkSelf($obj) { if ($obj instanceof self) { echo get_class($obj) . " is self<br>"; } } } class Test1 extends Test { } $test1Static = Test1::create("test1"); v($test1Static); //输出object(Test1)#1 (0) 因为通过Test1创建 $testStatic = Test::create("test"); v($testStatic); //输出object(Test)#2 (0) 因为通过Test创建 $test1Self = Test1::createSelf("test1"); v($test1Self); //输出object(Test)#3 (0) 因为是new self $testSelf = Test::createSelf("test"); v($testSelf); //输出object(Test)#4 (0) 因为是new self p();Test::checkStatic($test1Static); //第1次调用结果==>Test1 is static p();Test::checkStatic($testStatic); //第2次调用结果==>Test is static p();Test::checkStatic($test1Self); //第3次调用结果==>Test is static p();Test::checkStatic($testSelf); //第4次调用结果==>Test is static p();Test1::checkStatic($test1Static); //第5次调用结果==>Test1 is static //第6、7、8次checkStatic没有进入if,因为延迟静态绑定,new static实例化的是父类 //通过子类的调用去checkStatic是不会进入if条件的 p();Test1::checkStatic($testStatic); //第6次调用结果==> p();Test1::checkStatic($test1Self); //第7次调用结果==> p();Test1::checkStatic($testSelf); //第8次调用结果==> p();Test::checkSelf($test1Static); //第9次调用结果==>Test1 is self p();Test::checkSelf($testStatic); //第10次调用结果==>Test is self p();Test::checkSelf($test1Self); //第11次调用结果==>Test is self p();Test::checkSelf($testSelf); //第12次调用结果==>Test is self p();Test1::checkSelf($test1Static); //第13次调用结果==>Test1 is self p();Test1::checkSelf($testStatic); //第14次调用结果==>Test is self p();Test1::checkSelf($test1Self); //第15次调用结果==>Test is self p();Test1::checkSelf($testSelf); //第16次调用结果==>Test is self function v($param) { echo "<pre>"; echo "<br>"; var_dump($param); echo "</pre>"; } function p() { static $i; if (empty($i)) { $i = 1; } else { $i++; } echo "第{$i}次调用结果==>"; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
<?php //一个简单的demo,把pipeline理解为流水线,经过了2个工人的加工 class PipeLine { protected $passable; protected $pipes = []; protected $method = 'handle'; public function send($passable) { $this->passable = $passable; return $this; } public function through($pipes) { $this->pipes = is_array($pipes) ? $pipes : func_get_args(); return $this; } public function via($method) { $this->method = $method; return $this; } public function thenForeach() { foreach ($this->pipes as $object) { $object->{$this->method}($this->passable); } return $this->passable; } public function thenReduce() { array_reduce($this->pipes, $this->handle(), $this->passable); return $this->passable; } protected function handle() { return function ($stack, $pipe) { return call_user_func(array($pipe, $this->method), $stack); }; } } class Request { protected $params = array( 'userName' => 'zhangsan', 'age' => 500 ); protected $method = 'GET'; public function setParams($params) { $this->params = $params; } public function getParams() { return $this->params; } public function getMethod() { return $this->method; } } class Worker1 { public function handle($request) { $params = $request->getParams(); foreach ($params as $k => $v) { if ($k == 'age' && ($v > 100 || $v < 1)) { $params[$k] = ''; } if ($k == 'userName' && empty($v)) { $params[$k] = '游客'; } } $request->setParams($params); return $request; } } class Worker2 { public function handle($request) { $method = $request->getMethod(); if ($method == "GET") { $this->urlencode($request); } return $request; } protected function urlencode($request) { $params = $request->getParams(); foreach ($params as $k => $v) { $params[$k] = urlencode($v); } $request->setParams($params); } } $request1 = new Request; $request2 = new Request; $work1 = new Worker1; $work2 = new Worker2; $pipeline = new PipeLine; $pipeline->send($request1)->through([$work1, $work2])->via('handle')->thenForeach(); echo '<pre>'; print_r($request1->getParams()); $pipeline->send($request2)->through([$work1, $work2])->via('handle')->thenReduce(); print_r($request2->getParams()); |