PHP 8.4.1 Released!

ReflectionClass::getProperty

(PHP 5, PHP 7, PHP 8)

ReflectionClass::getPropertyクラスのプロパティを表す ReflectionProperty を取得する

説明

public ReflectionClass::getProperty(string $name): ReflectionProperty

クラスのプロパティを表す ReflectionProperty を取得します。

パラメータ

name

プロパティの名前。

戻り値

ReflectionProperty を返します。

例1 ReflectionClass::getProperty() の基本的な使用例

<?php
$class
= new ReflectionClass('ReflectionClass');
$property = $class->getProperty('name');
var_dump($property);
?>

上の例の出力は以下となります。

object(ReflectionProperty)#2 (2) {
  ["name"]=>
  string(4) "name"
  ["class"]=>
  string(15) "ReflectionClass"
}

参考

add a note

User Contributed Notes 2 notes

up
29
eric at naeseth dot com
13 years ago
If the class doesn't have a property with the given name, a ReflectionException will be raised.
up
9
dohpaz42
9 years ago
Accessing private properties is possible, but care must be taken if that private property was defined lower into the inheritance chain. For example, if class A extends class B, and class B defines a private property called 'foo', getProperty will throw a ReflectionException.

Instead, you can loop over getParentClass until it returns false to look for the private property, at which point you can access and/or modify its value as needed.
To Top