PHP 類型提示

王林
發布: 2024-08-29 13:03:37
原創
370 人瀏覽過

PHP 類型提示是您在 PHP 5 版本之後會遇到的最好的概念之一。我們都知道,在 PHP 程式中,我們不會使用 PHP 程式語言在 PHP 程式內部的任何位置輸入任何函數宣告、變數宣告或任何其他類型的宣告的特定資料類型。但在 PHP 5 和 PHP 5+ 版本之後,資料型別可以作為 PHP 函數的參數在 PHP 程式中傳遞。我們將此功能稱為類型提示。當需要在特定時間更改特定實例時,這非常有幫助。

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

文法

function printMenu1(Controller, $controller1){
//program statements
…
}
登入後複製

上述文法解釋:

在上述語法中,$controller1 變數暗示有類別名稱“Controller”,但 printMenu1() 只會接受 Controller 類別物件作為 Controller 參數。

類型提示在 PHP 中如何運作?

PHP 類型提示僅適用於某些類型的資料。對於函數arg/argument,使用者將指定一些使用者定義的類別實例,並透過類別名稱進行提示。 PHP 5版本後可以使用。它的工作原理是在 PHP 5 和 PHP 5+ 版本之後指定變數、函數等的資料類型。在 PHP 5 版本之前,PHP 類型提示概念甚至不起作用。

它也適用於可呼叫的介面、陣列和函數。它無法處理特定的對象,例如 float、int、string 和 Boolean 類型等。假設如果我們對 INT 資料類型使用 PHP 類型提示,則程式的執行將停止並導致錯誤例如「可捕獲的致命錯誤」等..

它有助於指定預期的資料類型。這些資料類型是特定函數宣告中特定參數的物件、陣列和介面等。這是最有利的事情之一,因為它將提供更好的程式碼結果,並透過減少錯誤訊息或改進錯誤訊息來改進。

PHP 類型提示範例

實作類型提示概念的範例。

範例1

這裡 calcNumMilesOnFullTank1() 將會接受 $models1 物件。然後使用 foreach 迴圈存取模型類別物件/陣列項目中的每個項目。然後使用 echo 語句存取第一個項目「item[0]」。但是這個 PHP 程式將會在輸出中回傳 Fatal error。這裡只將一個字串值加入函數中,而不是傳遞許多陣列元素等。這裡 item[1] 和 item[2] 必須相乘並且必須提供結果,但提供致命錯誤輸出。

文法:

<?php
// The function will only get array as an argument.
function calcNumMilesOnFullTank1(array $models1)
{
foreach($models1 as $item1)
{
echo $carModel1 = $item1[0];
echo " : ";
echo $numberOfMiles1 = $item1[1] * $item1[2];
echo "<br />";
}
}
calcNumMilesOnFullTank1("Toyota");
?>
登入後複製

輸出:

PHP 類型提示

範例2

這裡的integerTest1()函數接受類別物件「val1」。這是實作 PHP 程式語言的類型提示概念的範例。在 PHP 程式標籤內部建立了一個函數,然後建立了 echo 語句來列印變數「val1」的值。然後在關閉函數的括號後,呼叫integerTest1(12)將整數值「12」傳遞給函數,然後關閉PHP標籤。編譯此 PHP 程式後,將會得到輸出“12”,因為整數值“12”會傳遞給函數的物件。

文法:

<?php
function integerTest1(int $val1) {
echo $val1;
}
integerTest1(12);
?>
登入後複製

輸出:

PHP 類型提示

範例3

這是實作 PHP 程式語言的陣列類型提示的範例。這裡首先使用陣列類別物件 $colors1 建立 showColors1 函數,然後使用 echo 語句列印一些字串測試。然後使用 FOREACH 概念來列印放置在一個/多個陣列內部的 color1 值。然後建立ECHO語句來列印colors1數組的col1值。然後函數的括號被關閉,然後建立 $colors1 陣列變數來儲存一些字串值。它們可以是顏色或任何其他顏色。人們可以根據我們的要求將任何類型的元素/項目放置在特定數組內。然後呼叫 showColors1() 來運行整個函數/方法語句。然後 PHP 標籤關閉。查看輸出並了解要列印的元素是什麼。

文法:

<?php
function showColors1(array $colors1)
{
echo "List of the colors=>";
foreach($colors1 as $col1)
{
echo "\n".$col1;
}
}
$colors1 = ['Red','Violet','Green','orange','Blue','Yellow'];
showColors1($colors1);
?>
登入後複製

輸出:

PHP 類型提示

Example 4

This is the PHP Program of implementing the PHP Type Hinting. At first, a class called pavankumarsake is created and then inside of the class add() function is created and had 2 parameters. They are a and b variables and then the sum of those two will be printed/returned. Then closing of the parenthesis is done and then a new class is created to store the value of the two variables. Then calc1 and test 1 variable are created to call pavankumarsake() and test1() classes. This program will provide the sum of the two integer values of the specific variables. Check out the output so that you will know better.

Syntax:

<?php
class pavankumarsake
{
public function add($a1, $b1)
{
return $a1 + $b1;
}
}
class test1
{
public function __construct(pavankumarsake $sum1)
{
echo "the sum of two values 2 and 3 is = ".$sum1->add(2, 3);
}
}
$calc1 = new pavankumarsake();
$test1 = new test1($calc1);
?>
登入後複製

Output:

PHP 類型提示

Advantages

There are some advantages of using the PHP Type Hinting concept in the PHP Program writing or editin5g. Most of them are listed below. Checkout once.

  • With the help of Type Hinting, one can know the specific expected type of argument of the specific function only at the time of the maintenance.
  • The Function declaration of PHP and its usage would be perfect and obvious after transferring the code to some other developer of PHP language.
  • This makes the PHP code more useful to get some future reference/references.

Conclusion

I hope you learned what is the definition of PHP Type Hinting along with its syntax and explanation, How the Type Hinting works in PHP Programming Language along with various examples to under the PHP TYPE HINTING concept, Advantages of PHP Type Hinting, etc.

以上是PHP 類型提示的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
php
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!