Blogger Information
Blog 20
fans 0
comment 0
visits 8098
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
类的扩展和命名空间
P粉191340380
Original
291 people have browsed it

类的扩展

  1. // __get()
  2. class user
  3. {
  4. private array $data =[
  5. 'age' => 18,
  6. ];
  7. public function __get($name)
  8. {
  9. $a = array_key_exists($name, $this->data);
  10. return $a ? $this->data[$name] : "$name 属性不存在" . '<br>';
  11. }
  12. // set()
  13. public function __set ($name,$value) {
  14. $this->$name = $value;
  15. }
  16. // __call()
  17. public function __call($name, $age)
  18. {
  19. printf('%s , %s', $name, $age);
  20. }
  21. // __callstatic
  22. public static function __callstatic($name, $age)
  23. {
  24. printf('%s, %s', $name, $age);
  25. }
  26. }
  27. class stu
  28. {
  29. protected string $name;
  30. private $age = 18;
  31. // __construct()
  32. public function __construct($name)
  33. {
  34. $this -> name =$name;
  35. }
  36. }

命名空间

  1. namespace one;
  2. class a
  3. {
  4. public static function a1()
  5. {
  6. return __METHOD__;
  7. }
  8. }
  9. echo a::a1() . '<br>';
  10. echo two\a::a1() . '<br>';
  11. echo \one\two\a::a1() . '<br>';
  12. use \one\two\three\a as UserIndex;
  13. echo a::a1() . '<br>';
  14. namespace one\two;
  15. class a
  16. {
  17. public static function a1()
  18. {
  19. return __METHOD__;
  20. }
  21. }
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!