PHP Conference Japan 2024

ReflectionType::__toString

(PHP 7, PHP 8)

ReflectionType::__toString轉換為字串

說明

public ReflectionType::__toString(): 字串

取得參數類型名稱。

參數

此函式沒有參數。

回傳值

回傳參數的類型。

更新日誌

版本 說明
8.0.0 ReflectionType::__toString() 已取消棄用。
7.1.0 ReflectionType::__toString() 已棄用。

範例

範例 #1 ReflectionType::__toString() 範例

<?php
function someFunction(string $param) {}

$reflectionFunc = new ReflectionFunction('someFunction');
$reflectionParam = $reflectionFunc->getParameters()[0];

echo
$reflectionParam->getType();

上述範例的輸出結果類似如下:

string

另請參閱

新增註釋

使用者貢獻的註釋 1 則註釋

匿名
4 年前
關於此方法的棄用

ReflectionType::__toString() 最初在 PHP 7.1.0 alpha1 中被棄用。
棄用通知在 PHP 7.1.0 RC3 中被移除,然後在 PHP 7.4.0 alpha1 中恢復。

從 PHP 7.1.0 beta 3 開始,ReflectionParameter::getType() 和 ReflectionFunctionAbstract::getReturnType() 返回 ReflectionNamedType 的實例,而不是 ReflectionType。
ReflectionNamedType 類繼承自 ReflectionType,但提供了一個額外的 getName() 方法,可用於檢索類型提示。

最後,PHP 8.0.0 alpha1 引入了聯合類型的概念(參見 https://wiki.php.net/rfc/union_types_v2)。因此,ReflectionParameter::getType() 和 ReflectionFunctionAbstract::getReturnType() 現在將返回 ReflectionNamedType 或 ReflectionUnionType 的實例,具體取決於類型提示,兩者都是 ReflectionType 的子類。
To Top