PHP Conference Japan 2024

ReflectionException 類別

(PHP 5, PHP 7, PHP 8)

簡介

ReflectionException 類別。

類別概要

class ReflectionException extends Exception {
/* 繼承的屬性 */
protected string $message = "";
private string $string = "";
protected int $code;
protected string $file = "";
protected int $line;
private array $trace = [];
private ?Throwable $previous = null;
/* 繼承的方法 */
public Exception::__construct(string $message = "", int $code = 0, ?Throwable $previous = null)
final public Exception::getCode(): int
final public Exception::getFile(): string
final public Exception::getLine(): int
final public Exception::getTrace(): array
}
新增筆記

使用者貢獻的筆記 1 則筆記

11
temirkhan.nasukhov
5 年前
請注意,即使此例外不是基於 RuntimeException,它也是未檢查的。此類錯誤必須導致程式碼中的直接修復,而不是在處理情況的 catch 中修復。

另請記住,IDE(我的 IDE 是 Phpstorm 2018.3.4)的檢查會因此而突出顯示它,因此不要為任何 Runtime 例外建立帶有 throws 的 phpdoc。

<?php

/**
* 這種方式是錯誤的
*
* @throws \ReflectionException
*/
function createReflection()
{
return new
\ReflectionClass('invalid argument');
}

/**
* 這種方式是可以的(我當然不是指程式碼)
*/
function createReflection()
{
return new
\ReflectionClass('invalid argument');
}
To Top