Blogger Information
Blog 25
fans 0
comment 0
visits 10607
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
实例演示函数,类与对象的相关知识点
PHui
Original
485 people have browsed it

1.函数

  1. <?php
  2. //todo 函数
  3. // 1.声明
  4. function sum(int $a, int $b): string
  5. {
  6. return vsprintf("%d + %d =%d\n", [$a, $b, $a + $b]);
  7. }
  8. // 2.调用
  9. echo sum(10, 20);
  10. // 3.参数
  11. function sum1(int $a, int $b, int $c = 0): int
  12. {
  13. return array_sum([$a, $b, $c]);
  14. }
  15. echo sum1(1, 2, 3), "\n";
  16. // 3.1 参数太少,默认参数
  17. echo sum1(1, 2), "\n";
  18. // 3.2参数太多,可变参数
  19. function sum2(...$args): int
  20. {
  21. return array_sum($args);
  22. }
  23. echo sum2(1, 2, 3, 4, 5, 6), "\n";
  24. $arr = range(1, 6);
  25. // ...rest在调用的时候,执行的不是压入.而是展开
  26. echo sum2(...$arr), "\n";
  27. // 3.3 引用参数
  28. // '&'引用参数地址
  29. function sum3(&$a)
  30. {
  31. return $a += 10;
  32. }
  33. $a = 10;
  34. echo sum3($a), "\n";
  35. echo '$a =' . $a, "\n";
  36. // 4.返回值
  37. // 默认单值, 多值返回数组
  38. function fn1()
  39. {
  40. // return ['admin', 'admin@php.cn'];
  41. return ['name' => 'zhulaoshi', 'email' => 'zhulaoshi@php.cn'];
  42. }
  43. print_r(fn1());
  44. // 解构
  45. ['name' => $name, 'email' => $email] = fn1();
  46. echo "$name ( $email ) \n";
  47. // 回调函数
  48. echo call_user_func('sum2', 1, 2, 3, 4, 5, 6) . "\n";
  49. echo call_user_func_array('sum2', [1, 2, 3, 4, 5, 6]);

2.对象与类

  1. <?php
  2. // 类与对象
  3. /**
  4. * class: 声明类
  5. * new: 类的实例化
  6. * public: 访问限制符(公开成员/默认值)
  7. * private: 访问限制符(私有成员,仅在类中访问,类外不可见)
  8. * __construct(): 构造方法(实例化时调用,用于初始化类实例)
  9. * $this: 当前类实例的引用,只能用在类中
  10. * __get($name): 属性重载,拦截非法属性"读"访问
  11. * __set($name,$value): 属性重载,拦截非法属性"写"访问
  12. * __call($name,$arguments): 方法重载,拦截非法的方法访问
  13. */
  14. // class: 声明类
  15. class Demo
  16. {
  17. // 属性: 变量
  18. // 访问限制符: public公开成员(默认值)
  19. public string $name;
  20. private int $salary;
  21. private int $age;
  22. // 方法: 函数
  23. // 构造方法: 在类实例化时自动调用
  24. // __method: 魔术方法,用户不能直接调用,由PHP调用
  25. // 用途: 创建实例初始化状态,属性赋值,调用方法
  26. public function __construct($name, $salary, $age)
  27. {
  28. $this->name = $name;
  29. $this->salary = $salary;
  30. $this->age = $age;
  31. }
  32. // 属性重载, 拦截外部的非法属性访问
  33. // __get()获取器,当获取一个不存在或无权限访问的属性时触发
  34. public function __get($name)
  35. {
  36. $result = 0;
  37. if ($name === 'salary') {
  38. $result = $this->$name - 5000;
  39. } else {
  40. $result = $this->$name;
  41. }
  42. return $result;
  43. }
  44. // __set(属性名,新值)
  45. public function __set($name, $value)
  46. {
  47. if ($name === 'salary') {
  48. $this->$name += $value;
  49. } else {
  50. $this->$name = $value;
  51. }
  52. }
  53. // 格式化打印
  54. private function hello()
  55. {
  56. return <<< DATA
  57. $this->name :
  58. 工资($this->salary)
  59. 年龄($this->age)
  60. DATA;
  61. }
  62. // 方法重载
  63. public function __call($name, $arguments)
  64. {
  65. if ($name === 'hello') {
  66. return <<< DATA
  67. $this->name :
  68. 工资($this->salary)
  69. 年龄($this->age)
  70. DATA;
  71. }
  72. if ($name === 'sum') {
  73. return array_sum($arguments);
  74. }
  75. }
  76. }
  77. // 类实例化: 对象
  78. $obj = new Demo('王老师', 8000, 30);
  79. echo $obj->salary . "\n";
  80. $obj->salary = 2000;
  81. echo $obj->salary, "\n";
  82. $obj->age = 40;
  83. echo $obj->age, "\n";
  84. echo $obj->hello(), "\n";
  85. // call_user_func
  86. // call_user_func_array
  87. // 用它调用对象的方法
  88. echo call_user_func([$obj, 'sum'], 1, 2, 3) . "\n";
  89. echo call_user_func_array([$obj, 'sum'], [1, 2, 3]) . "\n";

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