Blogger Information
Blog 27
fans 1
comment 2
visits 90437
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
函数,类与对象中的关键字
          
Original
450 people have browsed it
  1. <?php
  2. //作业内容:
  3. // 1. 实例演示函数的相关知识点
  4. // 2. 实例演示课堂上提及全部类与对象的关键字,重点放在属性与方法的重载上
  5. //申明函数 PHP7.1以后,类型约定
  6. function hello(int $a, int $b) : int
  7. // :int 返回值必须是数值 没有return写:void待定
  8. {
  9. // 获取传递的参数
  10. func_get_args();
  11. print_r(func_get_args());
  12. return "$a + $b";
  13. }
  14. //函数调用
  15. echo hello(22,2)."\n";
  16. class Person
  17. {
  18. // 属性
  19. private string $act;
  20. // 私有属性
  21. private int $age;
  22. private string $name;
  23. private int $salary;
  24. // 构造方法,实例初始化自动调用
  25. public function __construct($act,$age,$name,$salary)
  26. {
  27. $this->act = $act;
  28. $this->age = $age;
  29. $this->name = $name;
  30. $this->salary=$salary;
  31. }
  32. // 属性重载, 拦截外部的非法属性访问
  33. // __get()获取器,当获取一个不存在或【无权限访问】的属性时触发
  34. public function __get($propertyName){
  35. $result = 0;
  36. if ($propertyName === 'age'){
  37. $result = $this->age +5;
  38. }elseif ($propertyName === 'salary' ){
  39. $result = $this->salary+2000;
  40. }elseif ($propertyName === 'name'){
  41. $result = $this->name.'--->名儿';
  42. }elseif ($propertyName === 'act'){
  43. $result = $this->act;
  44. }
  45. return $result;
  46. }
  47. // __set(value): 属性设置器(写)
  48. // __set(属性名,新值)
  49. public function __set($name,$value){
  50. $this->$name = $value;
  51. }
  52. private function message($a,$b,$c)
  53. {
  54. return <<<DATA
  55. "$this->name,年龄:$this->age 技能:$this->act,工资:$this->salary"
  56. DATA;
  57. }
  58. // 普通方法重载: __call()
  59. public function __call($name,$arguments) // $name: 方法名称, $arguments: 参数数组
  60. {
  61. // return $name.','.join(',',$arguments);
  62. if ($name === 'ok'){
  63. echo '此方法不存在,啥也不做!';
  64. }elseif ($name === 'message'){
  65. return <<<DATA
  66. "$this->name,年龄:$this->age 技能:$this->act,工资:$this->salary"
  67. DATA;
  68. }
  69. }
  70. }
  71. $xiaoming = new Person('一指禅','18','王敏',3500);
  72. echo '姓名:'. $xiaoming->name.PHP_EOL;
  73. echo '年龄:'.$xiaoming->age.PHP_EOL;
  74. echo '工资+2千:' .$xiaoming->salary.PHP_EOL;
  75. echo'--------------'.PHP_EOL;
  76. // set设置属性
  77. $xiaoming->act = '降龙十八掌'.PHP_EOL;
  78. echo $xiaoming->act.PHP_EOL;
  79. echo $xiaoming->message(1,3,5).PHP_EOL;
  80. echo $xiaoming->ok(); // 没有这个方法
  81. /**
  82. * class: 声明类
  83. * new: 类的实例化
  84. * public: 访问限制符(公开成员/默认值)
  85. * private: 访问限制符(私有成员,仅在类中访问,类外不可见)
  86. * __construct(): 构造方法(实例化时调用,用于初始化类实例)
  87. * $this: 当前类实例的引用,只能用在类中
  88. * __get($name): 属性重载,拦截非法属性"读"访问
  89. * __set($name,$value): 属性重载,拦截非法属性"写"访问
  90. * __call($name,$arguments): 方法重载,拦截非法的方法访问
  91. */

运行结果

Correcting teacher:PHPzPHPz

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
Author's latest blog post