TestPhpLang.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2018/1/6
  6. * Time: 下午10:55
  7. */
  8. class Cart
  9. {
  10. const PRICE_BUTTER = 1.00;
  11. const PRICE_MILK = 3.00;
  12. const PRICE_EGGS = 6.95;
  13. protected $products = array();
  14. public function add($product, $quantity)
  15. {
  16. $this->products[$product] = $quantity;
  17. }
  18. public function getQuantity($product)
  19. {
  20. return isset($this->products[$product]) ? $this->products[$product] : FALSE;
  21. }
  22. public function getTotal($tax)
  23. {
  24. $total = 0.00;
  25. $callback = function ($quantity, $product) use ($tax, &$total) {
  26. $pricePerItem = constant(__CLASS__ . "::PRICE_" . strtoupper($product));
  27. $total += ($pricePerItem * $quantity) * ($tax + 1.0);
  28. };
  29. array_walk($this->products, $callback);
  30. return round($total, 2);
  31. }
  32. }
  33. class Test
  34. {
  35. public function testing()
  36. {
  37. return function() {
  38. var_dump($this);
  39. };
  40. }
  41. }
  42. class A
  43. {
  44. public static $ClassNameX = 'HelloA';
  45. function __construct($val) {
  46. $this->val = $val;
  47. }
  48. function getClosure() {
  49. //returns closure bound to this object and scope
  50. return function() { return $this->val; };
  51. }
  52. }
  53. class StaticTest
  54. {
  55. public function __call($method, $parameters)
  56. {
  57. if($method == "where") {
  58. echo json_encode($parameters);
  59. }
  60. elseif($method == "field") {
  61. echo json_encode($parameters);
  62. }
  63. return $this;
  64. }
  65. public function select()
  66. {
  67. echo "select";
  68. }
  69. /**
  70. * Dynamically pass methods to the default connection.
  71. *
  72. * @param string $method
  73. * @param array $parameters
  74. * @return mixed
  75. */
  76. public static function __callStatic($method, $parameters)
  77. {
  78. if($method == "where") {
  79. $obj = new StaticTest();
  80. return $obj->where($method,$parameters);
  81. }
  82. }
  83. }
  84. class TestPhpLang extends PHPUnit_Framework_TestCase
  85. {
  86. public function testCart()
  87. {
  88. $my_cart = new Cart;
  89. // 往购物车里添加条目
  90. $my_cart->add('butter', 1);
  91. $my_cart->add('milk', 3);
  92. $my_cart->add('eggs', 6);
  93. // 打出出总价格,其中有 5% 的销售税.
  94. print $my_cart->getTotal(0.05) . "\n";
  95. }
  96. public function testClass()
  97. {
  98. $object = new Test;
  99. $function = $object->testing();
  100. $function();
  101. }
  102. public function testBlock()
  103. {
  104. $message = 'hello';
  105. $example = function () use($message){
  106. var_dump($message);
  107. };
  108. $example();
  109. $example = function () use (&$message) {
  110. var_dump($message);
  111. };
  112. $this->block(function () {
  113. echo "hello world";
  114. });
  115. }
  116. private function block(callable $func)
  117. {
  118. $func();
  119. }
  120. public function testBind()
  121. {
  122. $func = function() {
  123. echo __METHOD__;
  124. };
  125. $func = $func->bindTo(new StdClass);
  126. $func();
  127. }
  128. public function testClosure()
  129. {
  130. $st_cl = function() {
  131. return self::$ClassNameX;
  132. };
  133. $mem_cl = function() {
  134. return $this->val;
  135. };
  136. $ob1 = new A(1);
  137. $ob2 = new A(2);
  138. echo A::class;
  139. $cl2 = Closure::bind($st_cl,NULL,A::class);
  140. echo $cl2();
  141. $cl = $mem_cl->bindTo($ob1);
  142. echo $cl();
  143. }
  144. public function testStaticCall()
  145. {
  146. StaticTest::where(['i am here'])->field()->select();
  147. }
  148. function array_insert (&$array, $pos, $new)
  149. {
  150. if(is_array($new)) {
  151. $x = array_slice( $array, 0, $pos );
  152. $x[] = $new;
  153. $y = array_slice( $array, $pos );
  154. $array = array_merge( $x, $y );
  155. }
  156. else {
  157. }
  158. }
  159. }