products[$product] = $quantity; } public function getQuantity($product) { return isset($this->products[$product]) ? $this->products[$product] : FALSE; } public function getTotal($tax) { $total = 0.00; $callback = function ($quantity, $product) use ($tax, &$total) { $pricePerItem = constant(__CLASS__ . "::PRICE_" . strtoupper($product)); $total += ($pricePerItem * $quantity) * ($tax + 1.0); }; array_walk($this->products, $callback); return round($total, 2); } } class Test { public function testing() { return function() { var_dump($this); }; } } class A { public static $ClassNameX = 'HelloA'; function __construct($val) { $this->val = $val; } function getClosure() { //returns closure bound to this object and scope return function() { return $this->val; }; } } class StaticTest { public function __call($method, $parameters) { if($method == "where") { echo json_encode($parameters); } elseif($method == "field") { echo json_encode($parameters); } return $this; } public function select() { echo "select"; } /** * Dynamically pass methods to the default connection. * * @param string $method * @param array $parameters * @return mixed */ public static function __callStatic($method, $parameters) { if($method == "where") { $obj = new StaticTest(); return $obj->where($method,$parameters); } } } class TestPhpLang extends PHPUnit_Framework_TestCase { public function testCart() { $my_cart = new Cart; // 往购物车里添加条目 $my_cart->add('butter', 1); $my_cart->add('milk', 3); $my_cart->add('eggs', 6); // 打出出总价格,其中有 5% 的销售税. print $my_cart->getTotal(0.05) . "\n"; } public function testClass() { $object = new Test; $function = $object->testing(); $function(); } public function testBlock() { $message = 'hello'; $example = function () use($message){ var_dump($message); }; $example(); $example = function () use (&$message) { var_dump($message); }; $this->block(function () { echo "hello world"; }); } private function block(callable $func) { $func(); } public function testBind() { $func = function() { echo __METHOD__; }; $func = $func->bindTo(new StdClass); $func(); } public function testClosure() { $st_cl = function() { return self::$ClassNameX; }; $mem_cl = function() { return $this->val; }; $ob1 = new A(1); $ob2 = new A(2); echo A::class; $cl2 = Closure::bind($st_cl,NULL,A::class); echo $cl2(); $cl = $mem_cl->bindTo($ob1); echo $cl(); } public function testStaticCall() { StaticTest::where(['i am here'])->field()->select(); } function array_insert (&$array, $pos, $new) { if(is_array($new)) { $x = array_slice( $array, 0, $pos ); $x[] = $new; $y = array_slice( $array, $pos ); $array = array_merge( $x, $y ); } else { } } }