PHP - 型別提示



PHP 支援在函式定義和類中的屬性或例項變數宣告時使用“型別提示”。PHP 被廣泛認為是一種弱型別語言。在 PHP 中,無需在為變數賦值之前宣告其型別。

PHP 解析器會盡力將變數轉換為相容的型別。因此,如果傳遞的值之一是數字的字串表示形式,而第二個是數值變數,則 PHP 會將字串變數轉換為數值以執行加法運算。

示例

看看下面的例子:

<?php
   function addition($x, $y) {
      echo "First number: $x Second number: $y Addition: " . $x+$y;
   }
   $x="10";
   $y=20;
   addition($x, $y);
?>

它將產生以下輸出

First number: 10 Second number: 20 Addition: 30

但是,如果上面示例中的$x是一個不包含有效數字表示的字串,那麼您將遇到錯誤。

<?php
   function addition($x, $y) {
      echo "First number: $x Second number: $y Addition: " . $x+$y;
   }
   $x="Hello";
   $y=20;
   addition($x, $y);
?>

它將產生以下輸出

PHP Fatal error:  Uncaught TypeError: Unsupported operand types: string + int in hello.php:5

型別提示從 PHP 5.6 版本開始受支援。這意味著您可以顯式宣告程式碼中宣告的變數的預期型別。PHP 允許您對函式引數、返回值和類屬性進行型別提示。這樣,可以編寫更健壯的程式碼。

讓我們在上面的程式中的加法函式中加入型別提示:

function addition($x, $y) {
   echo "First number: $x Second number: $y Addition: " . $x+$y;
}

型別提示功能主要由 IDE(整合開發環境)使用,用於提示使用者函式宣告中使用的引數的預期型別。

下圖顯示了 VS Code 編輯器在您鍵入時彈出的函式原型:

PHP Type Hints 1

如果游標懸停在函式名稱上,則會顯示引數和返回值的型別宣告:

PHP Type Hints 2

請注意,僅僅在變數宣告中使用資料型別並不能防止不匹配的型別異常,因為 PHP 是一種動態型別語言。換句話說,$x="10" 和 $y=20 仍然會將結果相加為 30,而 $x="Hello" 會使解析器引發錯誤。

strict_types

PHP 可以強制執行更嚴格的型別轉換規則,以便不會將“10”隱式轉換為 10。這可以透過在 declare() 語句中將 strict_types 指令設定為 1 來強制執行。declare() 語句必須是 PHP 程式碼中的第一條語句,緊跟在“<?php”標籤之後。

示例

<?php
   declare (strict_types=1);
   function addition(int $x, int $y) {
      echo "First number: $x Second number: $y Addition: " . $x+$y;
   }
   $x=10;
   $y=20;
   addition($x, $y);
?>

它將產生以下輸出

First number: 10 Second number: 20 Addition: 30

現在,如果將$x設定為“10”,則不會發生隱式轉換,從而導致以下錯誤:

PHP Fatal error:  Uncaught TypeError: addition(): Argument #1 ($x) must be of type int, string given

VS Code IDE 也指示了相同效果的錯誤:

PHP Type Hints 3

從 PHP 7 開始,型別提示支援已擴充套件到函式返回值,以防止出現意外的返回值。您可以透過在引數列表後新增預期的型別來進行返回值型別提示,並在其前面加上冒號 (:) 符號。

示例

讓我們為上面加法函式的返回值新增型別提示:

<?php
   declare (strict_types=1);
   function addition(int $x, int $y) : int {
      return $x+$y;
   }

   $x=10;
   $y=20;

   $result = addition($x, $y);
   echo "First number: $x Second number: $y Addition: " . $result;
?>

同樣,如果發現函式返回的任何內容不是整數,IDE 甚至在您執行之前就會指出原因。

PHP Type Hints 4

聯合型別

PHP 在 8.0 版本中引入了聯合型別。您現在可以為單個宣告指定多個型別。資料型別之間用“|”符號分隔。

示例

在下面的 addition() 函式定義中,$x$y 引數可以是intfloat 型別。

<?php
   declare (strict_types=1);
   function addition(int|float $x, int|float $y) : float {
      return $x+$y;
   }
   $x=10.55;
   $y=20;

   $result = addition($x, $y);
   echo "First number: $x Second number: $y Addition: " . $result;
?>

類中的型別提示

在 PHP 中,從 7.4 版本開始,您可以在類屬性和方法的宣告中使用型別提示。

示例

在下面的示例中,類建構函式使用型別提示:

<?php
   declare (strict_types=1);
   class Student {
      public $name;
      public $age;
      public function __construct(string $name, int $age) {
         $this->name = $name;
         $this->age = $age;
      }

      public function dispStudent() {
         echo "Name: $this->name Age: $this->age";
      }
   }
   $s1 = new Student("Amar", 21);
   $s1->dispStudent();
?>

也可以在類屬性的宣告中使用型別提示。

class Student {
   public string $name;
   public int $age;

   public function __construct($name, $age) {
      $this->name = $name;
      $this->age = $age;
   }

   public function dispStudent() {
      echo "Name: $this->name Age: $this->age";
   }
}

程式開發過程中最常見的錯誤是型別錯誤。型別提示功能有助於減少此類錯誤。

廣告