查詢

SplFileInfo::__toString()函式—用法及示例

「 返回 SplFileInfo 物件的路徑和檔名的字串表示 」


函式名稱:SplFileInfo::__toString()

適用版本:PHP 5 >= 5.1.2, PHP 7

函式描述:SplFileInfo::__toString() 方法返回 SplFileInfo 物件的路徑和檔名的字串表示。

用法:

public __toString ( void ) : string

示例:

$fileInfo = new SplFileInfo('/path/to/file.txt');
echo $fileInfo; // 輸出:/path/to/file.txt

解釋: SplFileInfo::__toString() 方法是一個魔術方法,當將 SplFileInfo 物件用作字串時自動呼叫。它返回 SplFileInfo 物件的路徑和檔名的字串表示。

在上面的示例中,我們建立了一個 SplFileInfo 物件 $fileInfo,它代表路徑為 '/path/to/file.txt' 的檔案。然後,我們使用 echo 語句列印 $fileInfo 物件,由於 SplFileInfo 物件被用作字串,所以會自動呼叫 __toString() 方法,並將檔案的路徑和檔名作為字串輸出。

請注意,這個方法返回的是一個字串,而不是 SplFileInfo 物件本身。

補充糾錯
熱門PHP函式
分享連結