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

1.类的扩展

  1. __get()
  2. __set()
  3. __construct()
  4. __destruct()
  5. __call()
  6. __callStatic()
  1. class User{
  2. static $min = 10;
  3. private $data = [
  4. 'age' => 18,
  5. ];
  6. function __get($name)
  7. {
  8. return array_key_exists($name,$this->data) ? $this->data[$name] : $name . '不存在';
  9. }
  10. function __set($name,$value)
  11. {
  12. if(!array_key_exists($name,$this->data))
  13. {
  14. $this->data[$name] = $value;
  15. }
  16. else
  17. {
  18. return $name . '名字已经存在';
  19. }
  20. }
  21. function __call($name,$arge)
  22. {
  23. printf('%s,%s',$name,$arge);
  24. }
  25. static function __callStatic($name, $arguments)
  26. {
  27. // TODO: Implement __callStatic() method.
  28. echo $name;
  29. }
  30. }
  31. $user = new User();
  32. echo $user->name;
  33. $user->hellow(200);
  34. $user::word(100);
  35. echo User::$min;
  1. class query
  2. {
  3. function table($table)
  4. {
  5. return $this;
  6. }
  7. function where($table)
  8. {
  9. return $this;
  10. }
  11. function find()
  12. {
  13. }
  14. }
  15. class Db
  16. {
  17. static function __callStatic($name,$argement)
  18. {
  19. return call_user_func_array([new Query(),$name],$argement);
  20. }
  21. }
  22. Db::table('think_user')->where('id', 1)->find();

call_user_func_array():

1)普通使用:

  1. function a($b, $c) {
  2. echo $b;
  3. echo $c;
  4. }
  5. call_user_func_array('a', array("111", "222"));
  6. //输出 111 222

2)调用类内部的方法:

  1. Class ClassA {
  2. function bc($b, $c) {
  3. $bc = $b + $c;
  4. echo $bc;
  5. }
  6. }
  7. call_user_func_array(array('ClassA','bc'), array("111", "222"));
  8. //输出 333

2.接口类

  1. <?php
  2. //接口:抽象类和接口类似
  3. //接口类
  4. interface iUser
  5. {
  6. //类成员必须共有属性
  7. function fun1();
  8. function fun2();
  9. }
  10. //工作类
  11. class Demo01 implements iUser
  12. {
  13. function fun1()
  14. {
  15. }
  16. function fun2()
  17. {
  18. }
  19. }
  20. //PHP是单继承

3.接口类实现多继承

  1. <?php
  2. //接口:抽象类和接口类似
  3. //接口类
  4. interface iUser1
  5. {
  6. //类成员必须共有属性
  7. function fun1();
  8. function fun2();
  9. }
  10. interface iUser2
  11. {
  12. //类成员必须共有属性
  13. function fun3();
  14. function fun4();
  15. }
  16. //工作类
  17. class Demo01 implements iUser1,iUser2
  18. {
  19. function fun1()
  20. {
  21. echo __METHOD__;
  22. }
  23. function fun2()
  24. {
  25. echo __METHOD__;
  26. }
  27. function fun3()
  28. {
  29. // TODO: Implement fun3() method.
  30. echo __METHOD__;
  31. }
  32. function fun4()
  33. {
  34. // TODO: Implement fun4() method.
  35. echo __METHOD__;
  36. }
  37. }
  38. //PHP是单继承
  39. $user = new Demo01();
  40. $user->fun1();
  41. $user->fun3();

4.命名空间的定义

在PHP中函数、类、常量是不允许同名的。为了解决这三者的同名问题,所以出现了命名空间,在不同的命名空间中可以出现同名现象

5.命名空间的访问

  1. 非限定访问
  2. 限定访问
  3. 完全限定

    1. <?php
    2. /**
    3. * 命名空间
    4. *
    5. */
    6. namespace one;
    7. class A
    8. {
    9. //静态方法中不能引用非静态变量!
    10. //静态变量只能够类来引用
    11. static $name = 'yk';
    12. static function fun1()
    13. {
    14. return A::$name ;
    15. }
    16. }
    17. echo A::class; //非限定访问
    18. namespace two;
    19. class A
    20. {
    21. static function fun1()
    22. {
    23. }
    24. }
    25. echo A::class;
    26. echo \one\A::$name; //完全限定
    27. three\A::fun1(); //限定访问
    28. namespace two\three;
    29. class A
    30. {
    31. static function fun1()
    32. {
    33. echo __METHOD__;
    34. }
    35. }

    6.使用user

    1. <?php
    2. /**
    3. * 1.路径
    4. * 2.命名冲突问题
    5. * use默认使用完全限定名称/绝对路径
    6. * 3.
    7. */
    8. namespace one;
    9. class A
    10. {
    11. static $name = 'yk';
    12. public static function fun1()
    13. {
    14. return A::$name;
    15. }
    16. }
    17. use \one\two\A as B;
    18. B::fun1();
    19. namespace one\two;
    20. class A
    21. {
    22. static $name = 'yk';
    23. public static function fun1()
    24. {
    25. echo __METHOD__ . PHP_EOL;
    26. return A::$name;
    27. }
    28. }
    29. A::fun1();
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