ReflectionClass::getProperties

(PHP 5, PHP 7, PHP 8)

ReflectionClass::getPropertiesプロパティを取得する

説明

publicReflectionClass::getProperties(?int$filter = null): array

プロパティを取得します。

パラメータ

filter

オプションのフィルタで、取得したいプロパティの型を絞り込みます。 ReflectionProperty の定数 で設定し、デフォルトではすべてのプロパティ型を取得します。

戻り値

ReflectionProperty オブジェクトの配列を返します。

変更履歴

バージョン説明
7.2.0filter は、nullable になりました。

例1 ReflectionClass::getProperties() でのフィルタリングの例

この例では、オプションのパラメータ filter を使って private プロパティを読み飛ばします。

<?php
class Foo {
public
$foo = 1;
protected
$bar = 2;
private
$baz = 3;
}

$foo = new Foo();

$reflect = new ReflectionClass($foo);
$props = $reflect->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED);

foreach (
$props as $prop) {
print
$prop->getName() . "\n";
}

var_dump($props);

?>

上の例の出力は、 たとえば以下のようになります。

foo bar array(2) { [0]=> object(ReflectionProperty)#3 (2) { ["name"]=> string(3) "foo" ["class"]=> string(3) "Foo" } [1]=> object(ReflectionProperty)#4 (2) { ["name"]=> string(3) "bar" ["class"]=> string(3) "Foo" } }

参考

To Top