Blogger Information
Blog 25
fans 1
comment 1
visits 17375
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
0214作业+闭包与异常+10期线上班
江川林
Original
544 people have browsed it

闭包

将闭包绑定到对象上,来访问类成员
-创建要访问的类
-创建闭包函数
-用Closure类函数bindTobind进行绑定
-对闭包函数传参,继续类的访问

以下是bindTo()用法

  1. <?php
  2. namespace chapter4;
  3. use Closure;
  4. //创建一个类
  5. class Index
  6. {
  7. public $name = '笑傲江湖';
  8. public $book = '罗贯中';
  9. public function getInfo()
  10. {
  11. return $this->name . ' : ' . $this->book;
  12. }
  13. }
  14. $index = new Index();
  15. echo $index->getInfo();
  16. echo '<br>';
  17. //创建一个匿名函数
  18. $set = function (string $name,string $book) {
  19. $this->name = $name;
  20. $this->book = $book;
  21. };
  22. //将闭包绑定在对象上,并返回一个新闭包对象,来调用原对象
  23. $closure = $set->bindTo($index);
  24. //var_dump($closure);
  25. $closure('天龙八部', '乔峰');
  26. echo $index->getInfo();
  27. echo '<br>';
  28. //创建静态类
  29. class Index2
  30. {
  31. public static $name = '水浒传';
  32. public static $book = '罗贯中';
  33. public static function getInfo()
  34. {
  35. return static::$name . ' : ' . static::$book;
  36. }
  37. }
  38. $index2 = new Index2();
  39. echo $index2::getInfo();
  40. echo '<br>';
  41. //创建一个匿名函数
  42. $set = function (string $name,string $book) {
  43. static::$name = $name;
  44. static::$book = $book;
  45. };
  46. $closure = $set->bindTo(null,Index2::class);
  47. $closure('红楼梦', '贾宝玉');
  48. echo $index2->getInfo();

以下是bind()用法

  1. <?php
  2. namespace chapter4;
  3. use Closure;
  4. //使用Closure类里的bind()函数实现闭包对类属性的更新
  5. //创建一个类
  6. class Index3
  7. {
  8. public $name = '笑傲江湖';
  9. public $book = '罗贯中';
  10. public function getInfo()
  11. {
  12. return $this->name . ' : ' . $this->book;
  13. }
  14. }
  15. $index = new Index3;
  16. echo $index->getInfo();
  17. echo '<br>';
  18. //创建一个匿名函数
  19. $set = function (string $name,string $book) {
  20. $this->name = $name;
  21. $this->book = $book;
  22. };
  23. //将闭包绑定在对象上,来调用原对象
  24. //$closure = $set->bindTo($index);
  25. $closure = Closure::bind($set, $index);
  26. //var_dump($closure);
  27. $closure('天龙', '乔峰');
  28. echo $index->getInfo();
  29. echo '<br>';
  30. //创建静态类
  31. class Index4
  32. {
  33. public static $name = '水浒传';
  34. public static $book = '罗贯中';
  35. public static function getInfo()
  36. {
  37. return static::$name . ' : ' . static::$book;
  38. }
  39. }
  40. $index = new Index4();
  41. echo $index->getInfo();
  42. echo '<br>';
  43. //创建一个匿名函数
  44. $set = function (string $name,string $book) {
  45. static::$name = $name;
  46. static::$book = $book;
  47. };
  48. $closure = Closure::bind($set, $index);
  49. $closure('红楼', '贾宝玉');
  50. echo $index::getInfo();

异常

以下是异常处理与自定义异常

  1. <?php
  2. namespace chapter4;
  3. use Exception;
  4. try {
  5. if (true) throw new Exception('验证失败', 101);
  6. }catch (Exception $e){
  7. echo $e->getMessage() . '<br>';
  8. echo $e->getCode() . '<br>';
  9. echo $e->getFile() . '<br>';
  10. echo $e->getLine();
  11. }
  12. echo '<hr>';
  13. //自定义异常处理
  14. class Aexception extends Exception
  15. {
  16. public function __toString()
  17. {
  18. return <<<HEREDOC
  19. <table style="border: red 1px solid" border="1" cellpadding="0" cellspacing="5">
  20. <caption >异常提示内容</caption>
  21. <tr><th>Message</th><th>code</th><th>line</th></tr>
  22. <tr><td>$this->message</td><td>$this->code</td><td>$this->line</td></tr>
  23. </table>
  24. HEREDOC;
  25. }
  26. }
  27. try {
  28. if (true) throw new Aexception('验证登录失败,请重新查证后再次尝试', '888');
  29. }catch (Aexception $e){
  30. echo $e;
  31. }
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!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments