123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- <?php
- /**
- * Created by PhpStorm.
- * User: stanley-king
- * Date: 2018/1/6
- * Time: 下午10:55
- */
- class Cart
- {
- const PRICE_BUTTER = 1.00;
- const PRICE_MILK = 3.00;
- const PRICE_EGGS = 6.95;
- protected $products = array();
- public function add($product, $quantity)
- {
- $this->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 {
- }
- }
- }
|