首页 > 开发 > PHP > 正文

PHP类型约束的详细介绍(附代码)

2024-05-04 22:02:02
字体:大 中 小
来源:转载
供稿:网友

本篇文章给大家带来的内容是关于PHP类型约束的详细介绍(附代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

导语:所谓类型约束,即定义一个变量的时候,必须指定其类型,并且以后该变量也只能存储该类型数据。PHP 虽然是弱类型语言,但是在 PHP 5 已经支持类型约束,包括对象、接口、数组,在 PHP 7 之后支持标量类型约束,下面简单写几个示例。

标量类型、数组

在参数中指明类型,如果不一致,会抛出一个可捕获的致命错误

  1. <?php 
  2.  
  3. /** 
  4.  
  5.  * 数组类型约束 
  6.  
  7.  * @param array $arr 
  8.  
  9.  */ 
  10.  
  11. function printArray(array $arr) 
  12.  
  13. { 
  14.  
  15.     echo implode(',', $arr); 
  16.  
  17. } 
  18.  
  19.  
  20.  
  21. printArray(array(1, 2, 3));// 1,2,3 
  22.  
  23. printArray('1');// Fatal error: Uncaught TypeError: Argument 1 passed to printArray() must be of the type array, string given, called in D:/WWW/test.php on line 13 and defined in D:/WWW/test.php:7 Stack trace: #0 D:/WWW/test.php(13): printArray('1') #1 {main} thrown in D:/WWW/test.php on line 7 

如上所示,标量类型也是如此

  1. <?php 
  2.  
  3. /** 
  4.  
  5.  * 标量类型约束 
  6.  
  7.  * @param string $name 
  8.  
  9.  * @param int $age 
  10.  
  11.  * @param float $height 
  12.  
  13.  * @param bool $isBoy 
  14.  
  15.  */ 
  16.  
  17. function sayInfo(string $name, int $age, float $height, bool $isBoy) 
  18.  
  19. { 
  20.  
  21.     echo '姓名:' . $name . ',年龄:' . $age . ',身高:' . $height . ',是否为男孩:' . ($isBoy ? '是' : '否'); 
  22.  
  23. } 
  24.  
  25.  
  26.  
  27. sayInfo('tom', 12, 134.5, true);// 姓名:tom,年龄:12,身高:134.5,是否为男孩:是 

对象、接口

类型约束也可以指定为对象或者接口。首先定义一个 Human 接口,Boy 和 Girl 两个类分别实现接口

  1. <?php 
  2.  
  3. /** 
  4.  
  5.  * 接口 
  6.  
  7.  * Interface Human 
  8.  
  9.  */ 
  10.  
  11. interface Human 
  12.  
  13. { 
  14.  
  15.     public function say(); 
  16.  
  17.  
  18.  
  19.     public function run(); 
  20.  
  21. } 
  22.  
  23.  
  24.  
  25. /** 
  26.  
  27.  * 实现 Human 接口 
  28.  
  29.  * Class Boy 
  30.  
  31.  */ 
  32.  
  33. class Boy implements Human 
  34.  
  35. { 
  36.  
  37.     public function say() 
  38.  
  39.     { 
  40.  
  41.         echo 'a boy say'; 
  42.  
  43.     } 
  44.  
  45.  
  46.  
  47.     public function run() 
  48.  
  49.     { 
  50.  
  51.         echo 'a boy run'; 
  52.  
  53.     } 
  54.  
  55. } 
  56.  
  57.  
  58.  
  59. /** 
  60.  
  61.  * 实现 Human 接口 
  62.  
  63.  * Class Girl 
  64.  
  65.  */ 
  66.  
  67. class Girl implements Human 
  68.  
  69. { 
  70.  
  71.     public function say() 
  72.  
  73.     { 
  74.  
  75.         echo 'a girl say'; 
  76.  
  77.     } 
  78.  
  79.  
  80.  
  81.     public function run() 
  82.  
  83.     { 
  84.  
  85.         echo 'a girl run'; 
  86.  
  87.     } 
  88.  
  89. } 

接下来新建一个类来测试

  1. <?php 
  2.  
  3. include './human.php'; 
  4.  
  5.  
  6.  
  7. class Action 
  8.  
  9. { 
  10.  
  11.     /** 
  12.  
  13.      * Boy 对象类型约束 
  14.  
  15.      * @param Boy $boy 
  16.  
  17.      */ 
  18.  
  19.     public function boySay(Boy $boy) 
  20.  
  21.     { 
  22.  
  23.         $boy->say(); 
  24.  
  25.     } 
  26.  
  27.  
  28.  
  29.     /** 
  30.  
  31.      * Girl 对象类型约束 
  32.  
  33.      * @param Girl $girl 
  34.  
  35.      */ 
  36.  
  37.     public function girlSay(Girl $girl) 
  38.  
  39.     { 
  40.  
  41.         $girl->say(); 
  42.  
  43.     } 
  44.  
  45.  
  46.  
  47.     /** 
  48.  
  49.      * Human 接口类型约束 
  50.  
  51.      * @param Human $obj 
  52.  
  53.      */ 
  54.  
  55.     public function humanRun(Human $obj) 
  56.  
  57.     { 
  58.  
  59.         $obj->run(); 
  60.  
  61.     } 
  62.  
  63. } 
  64.  
  65.  
  66.  
  67. $obj = new Action(); 
  68.  
  69. $obj->boySay(new Boy());// a boy say 
  70.  
  71. echo '<br />'; 
  72.  
  73. $obj->girlSay(new Girl());// a girl say 
  74.  
  75. echo '<br />'; 
  76.  
  77. $obj->humanRun(new Boy());// a boy run 
  78.  
  79. echo '<br />'; 
  80.  
  81. $obj->humanRun(new Girl());// a girl run 

当类型约束为具体对象 Boy 或者 Girl 时,只能传入要求的对象。当类型约束为接口 Human 时,可以传入实现接口的类 Boy 或 Girl。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表