(PHP 8)
ReflectionProperty::hasDefaultValue — 檢查屬性是否已宣告預設值
此函式沒有參數。
範例 #1 ReflectionProperty::hasDefaultValue() 範例
<?php
class Foo {
public $bar;
public ?int $baz;
public ?int $foo = null;
public int $boing;
public function __construct()
{
$this->ping = '';
}
}
$ro = new ReflectionObject(new Foo());
var_dump($ro->getProperty('bar')->hasDefaultValue());
var_dump($ro->getProperty('baz')->hasDefaultValue());
var_dump($ro->getProperty('foo')->hasDefaultValue());
var_dump($ro->getProperty('boing')->hasDefaultValue());
var_dump($ro->getProperty('ping')->hasDefaultValue()); // 動態屬性
var_dump($ro->getProperty('pong')->hasDefaultValue()); // 未定義的屬性
?>
以上範例會輸出:
bool(true) bool(false) bool(true) bool(false) bool(false) Fatal error: Uncaught ReflectionException: Property Foo::$pong does not exist in example.php