Blogger Information
Blog 40
fans 0
comment 1
visits 24365
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
第10章 0222-重载,引用与命名空间,学心心得,笔记(属性、方法重载,命名空间)
努力工作--周工--Robin
Original
612 people have browsed it

示例运行截图

1、属性、方法重载,示例代码

  1. class Demo1 {
  2. //------属性重载------------------------------------
  3. private $data = [];
  4. public function __get($name)
  5. {
  6. if(isset($this->data[$name]))
  7. return $this->data[$name];
  8. else
  9. return "您调用的属性不存在...";
  10. }
  11. public function __set($name, $value)
  12. {
  13. $this->data[$name] = $value;
  14. }
  15. //------方法重载------------------------------------
  16. public function __call($name, $args)
  17. {
  18. if(method_exists($this, $name))
  19. $this->$name($args);
  20. else
  21. echo "您调用的方法不存在...", "<br>";
  22. }
  23. public static function __callStatic($name, $args)
  24. {
  25. if(function_exists($name))
  26. self::$name($args);
  27. else
  28. echo "您调用的方法不存在...", "<br>";
  29. }
  30. private function query($args) {
  31. echo "这里是,query()查询方法...", "<br>";
  32. }
  33. private function save($args) {
  34. echo "这里是,save()存储方法...", "<br>";
  35. }
  36. private static function link($args) {
  37. echo "这里是静态,self::link()连接方法...", "<br>";
  38. }
  39. }
  40. echo "<h4>------属性重载,示例演示------------------------------------</h4>";
  41. $obj = new Demo1();
  42. $obj->name = "刘德华";
  43. $obj->age = "60";
  44. $obj->job = "演员";
  45. echo "姓名:". $obj->name.", 年龄:". $obj->age. ", 职业:". $obj->job, "<br>";
  46. echo $obj->gender, "<br>";
  47. echo "<h4>------方法重载,示例演示------------------------------------</h4>";
  48. $obj->query();
  49. $obj->save();
  50. $obj->save123();
  51. Demo1::link("127.0.0.1", "mySql", "root", "root");

示例运行截图

2、命名空间,示例代码

  1. namespace {
  2. class Model {}
  3. echo Model::class, '<br>';
  4. }
  5. namespace www {
  6. class Model {}
  7. echo Model::class, '<br>';
  8. }
  9. namespace www\php {
  10. class Model {}
  11. echo Model::class, '<br>';
  12. }
  13. namespace www\php\cn {
  14. class Model {}
  15. echo Model::class, '<br>';
  16. }
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