ReflectionProperty ใ‚ฏใƒฉใ‚น

(PHP 5, PHP 7, PHP 8)

ใฏใ˜ใ‚ใซ

ReflectionProperty ใ‚ฏใƒฉใ‚นใฏ ใ‚ฏใƒฉใ‚นใฎใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃใซใคใ„ใฆใฎๆƒ…ๅ ฑใ‚’ๅ ฑๅ‘Šใ—ใพใ™ใ€‚

ใ‚ฏใƒฉใ‚นๆฆ‚่ฆ

class ReflectionProperty implements Reflector {
/* ๅฎšๆ•ฐ */
public const int IS_STATIC;
public const int IS_READONLY;
public const int IS_PUBLIC;
public const int IS_PROTECTED;
public const int IS_PRIVATE;
public const int IS_ABSTRACT;
public const int IS_PROTECTED_SET;
public const int IS_PRIVATE_SET;
public const int IS_VIRTUAL;
public const int IS_FINAL;
/* ใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃ */
public string $name;
public string $class;
/* ใƒกใ‚ฝใƒƒใƒ‰ */
public function __construct(object|string $class, string $property)
private function __clone(): void
public static function export(mixed $class, string $name, bool $return = ?): string
public function getAttributes(?string $name = null, int $flags = 0): array
public function getDefaultValue(): mixed
public function getDocComment(): string|false
public function getHooks(): array
public function getModifiers(): int
public function getName(): string
public function getRawValue(object $object): mixed
public function getSettableType(): ?ReflectionType
public function getType(): ?ReflectionType
public function getValue(?object $object = null): mixed
public function hasDefaultValue(): bool
public function hasHook(PropertyHookType $type): bool
public function hasHooks(): bool
public function hasType(): bool
public function isAbstract(): bool
public function isDefault(): bool
public function isDynamic(): bool
public function isFinal(): bool
public function isInitialized(?object $object = null): bool
public function isLazy(object $object): bool
public function isPrivate(): bool
public function isPrivateSet(): bool
public function isPromoted(): bool
public function isProtected(): bool
public function isProtectedSet(): bool
public function isPublic(): bool
public function isReadOnly(): bool
public function isStatic(): bool
public function isVirtual(): bool
#[\Deprecated]
public function setAccessible(bool $accessible): void
public function setRawValue(object $object, mixed $value): void
public function setRawValueWithoutLazyInitialization(object $object, mixed $value): void
public function setValue(?object $object, mixed $value): void
public function skipLazyInitialization(object $object): void
public function __toString(): string
}

ใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃ

name

ใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃๅใ€‚่ชญใฟ่พผใฟๅฐ‚็”จใงใ€ๆ›ธใ่พผใ‚‚ใ†ใจใ™ใ‚‹ใจ ReflectionException ใ‚’ใ‚นใƒญใƒผใ—ใพใ™ใ€‚

class

ใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃใŒๅฎš็พฉใ•ใ‚Œใฆใ„ใ‚‹ใ‚ฏใƒฉใ‚นๅใ€‚่ชญใฟ่พผใฟๅฐ‚็”จใงใ€ๆ›ธใ่พผใ‚‚ใ†ใจใ™ใ‚‹ใจ ReflectionException ใ‚’ใ‚นใƒญใƒผใ—ใพใ™ใ€‚

ๅฎš็พฉๆธˆใฟๅฎšๆ•ฐ

ReflectionProperty ใฎไฟฎ้ฃพๅญ

ReflectionProperty::IS_STATIC int

static ใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃใ‚’็คบใ—ใพใ™ใ€‚ PHP 7.4.0 ใ‚ˆใ‚Šๅ‰ใฎใƒใƒผใ‚ธใƒงใƒณใงใฏใ€ใ“ใฎๅ€คใฏ 1 ใงใ—ใŸใ€‚

ReflectionProperty::IS_READONLY int

readonly ใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃใ‚’็คบใ—ใพใ™ใ€‚ PHP 8.1.0 ไปฅ้™ใงๅˆฉ็”จๅฏ่ƒฝใงใ™ใ€‚

ReflectionProperty::IS_PUBLIC int

public ใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃใ‚’็คบใ—ใพใ™ใ€‚ PHP 7.4.0 ใ‚ˆใ‚Šๅ‰ใฎใƒใƒผใ‚ธใƒงใƒณใงใฏใ€ใ“ใฎๅ€คใฏ 256 ใงใ—ใŸใ€‚

ReflectionProperty::IS_PROTECTED int

protected ใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃใ‚’็คบใ—ใพใ™ใ€‚ PHP 7.4.0 ใ‚ˆใ‚Šๅ‰ใฎใƒใƒผใ‚ธใƒงใƒณใงใฏใ€ใ“ใฎๅ€คใฏ 512 ใงใ—ใŸใ€‚

ReflectionProperty::IS_PRIVATE int

private ใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃใ‚’็คบใ—ใพใ™ใ€‚ PHP 7.4.0 ใ‚ˆใ‚Šๅ‰ใฎใƒใƒผใ‚ธใƒงใƒณใงใฏใ€ใ“ใฎๅ€คใฏ 1024 ใงใ—ใŸใ€‚

ReflectionProperty::IS_ABSTRACT int
abstract ใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃใ‚’็คบใ—ใพใ™ใ€‚ PHP 8.4.0 ไปฅ้™ใงๅˆฉ็”จๅฏ่ƒฝใงใ™ใ€‚
ReflectionProperty::IS_PROTECTED_SET int
PHP 8.4.0 ไปฅ้™ใงๅˆฉ็”จๅฏ่ƒฝใงใ™ใ€‚
ReflectionProperty::IS_PRIVATE_SET int
PHP 8.4.0 ไปฅ้™ใงๅˆฉ็”จๅฏ่ƒฝใงใ™ใ€‚
ReflectionProperty::IS_VIRTUAL int
PHP 8.4.0 ไปฅ้™ใงๅˆฉ็”จๅฏ่ƒฝใงใ™ใ€‚
ReflectionProperty::IS_FINAL int
final ใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃใ‚’็คบใ—ใพใ™ใ€‚ PHP 8.4.0 ไปฅ้™ใงๅˆฉ็”จๅฏ่ƒฝใงใ™ใ€‚

ๆณจๆ„:

ใ“ใ‚Œใ‚‰ใฎๅฎšๆ•ฐใฎๅ€คใฏใ€PHP ใฎใƒใƒผใ‚ธใƒงใƒณใŒ็•ฐใชใ‚‹ใจๅค‰ๆ›ดใ•ใ‚Œใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ ใ“ใ‚Œใ‚‰ใฎๅ€คใ‚’็›ดๆŽฅ็”จใ„ใšใ€ๅธธใซๅฎšๆ•ฐใ‚’ไฝฟใ†ใ“ใจใ‚’ๆŽจๅฅจใ—ใพใ™ใ€‚

ๅค‰ๆ›ดๅฑฅๆญด

ใƒใƒผใ‚ธใƒงใƒณ ่ชฌๆ˜Ž
8.4.0 ใ‚ฏใƒฉใ‚นๅฎšๆ•ฐใŒๅž‹ไป˜ใ‘ใ•ใ‚Œใพใ—ใŸใ€‚
8.4.0 ReflectionProperty::IS_VIRTUALใ€ ReflectionProperty::IS_PRIVATE_SETใ€ ReflectionProperty::IS_PROTECTED_SETใ€ ReflectionProperty::IS_ABSTRACTใ€ ReflectionProperty::IS_FINAL ใŒ่ฟฝๅŠ ใ•ใ‚Œใพใ—ใŸใ€‚
8.0.0 ReflectionProperty::export() ใฏใ€ๅ‰Š้™คใ•ใ‚Œใพใ—ใŸใ€‚

็›ฎๆฌก

  • ReflectionProperty::__clone โ€” ใ‚ฏใƒญใƒผใƒณใ™ใ‚‹
  • ReflectionProperty::__construct โ€” ReflectionProperty ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚’ไฝœๆˆใ™ใ‚‹
  • ReflectionProperty::export โ€” ใ‚จใ‚ฏใ‚นใƒใƒผใƒˆใ™ใ‚‹
  • ReflectionProperty::getAttributes โ€” ใ‚ขใƒˆใƒชใƒ“ใƒฅใƒผใƒˆใ‚’ๅ–ๅพ—ใ™ใ‚‹
  • ReflectionProperty::getDeclaringClass โ€” ๅฎฃ่จ€ใ—ใฆใ„ใ‚‹ใ‚ฏใƒฉใ‚นใ‚’ๅ–ๅพ—ใ™ใ‚‹
  • ReflectionProperty::getDefaultValue โ€” ใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃใงๅฎฃ่จ€ใ•ใ‚ŒใŸใƒ‡ใƒ•ใ‚ฉใƒซใƒˆๅ€คใ‚’่ฟ”ใ™
  • ReflectionProperty::getDocComment โ€” ใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใ‚ณใƒกใƒณใƒˆใ‚’ๅ–ๅพ—ใ™ใ‚‹
  • ReflectionProperty::getHook โ€” ๆŒ‡ๅฎšใ—ใŸใƒ•ใƒƒใ‚ฏใซๅฏพๅฟœใ™ใ‚‹ใƒชใƒ•ใƒฌใ‚ฏใ‚ทใƒงใƒณใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚’่ฟ”ใ™
  • ReflectionProperty::getHooks โ€” ใ“ใฎใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃใซๆŒ‡ๅฎšใ•ใ‚ŒใŸๅ…จใฆใฎใƒ•ใƒƒใ‚ฏใ‚’้…ๅˆ—ใง่ฟ”ใ™
  • ReflectionProperty::getModifiers โ€” ใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃใฎไฟฎ้ฃพๅญใ‚’ๅ–ๅพ—ใ™ใ‚‹
  • ReflectionProperty::getName โ€” ใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃๅใ‚’ๅ–ๅพ—ใ™ใ‚‹
  • ReflectionProperty::getRawValue โ€” get ใƒ•ใƒƒใ‚ฏใŒๅฎš็พฉใ•ใ‚Œใฆใ„ใŸใจใ—ใฆใ‚‚ใ€ใใ‚Œใ‚’่ฟ‚ๅ›žใ—ใฆใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃใฎๅ€คใ‚’่ฟ”ใ™
  • ReflectionProperty::getSettableType โ€” set ใƒ•ใƒƒใ‚ฏใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใฎๅž‹ใ‚’่ฟ”ใ™
  • ReflectionProperty::getType โ€” ใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃใฎๅž‹ใ‚’ๅ–ๅพ—ใ™ใ‚‹
  • ReflectionProperty::getValue โ€” ๅ€คใ‚’ๅ–ๅพ—ใ™ใ‚‹
  • ReflectionProperty::hasDefaultValue โ€” ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆๅ€คใŒๅฎฃ่จ€ใ•ใ‚Œใฆใ„ใ‚‹ใ‹ใ‚’ใƒใ‚งใƒƒใ‚ฏใ™ใ‚‹
  • ReflectionProperty::hasHook โ€” ใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃใซๆŒ‡ๅฎšใ•ใ‚ŒใŸใƒ•ใƒƒใ‚ฏใŒๅฎš็พฉใ•ใ‚Œใฆใ„ใ‚‹ใ‹ใฉใ†ใ‹ใ‚’่ฟ”ใ™
  • ReflectionProperty::hasHooks โ€” ใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃใซไฝ•ใ‹ใ—ใ‚‰ใฎใƒ•ใƒƒใ‚ฏใŒๅฎš็พฉใ•ใ‚Œใฆใ„ใ‚‹ใ‹ใฉใ†ใ‹ใ‚’่ฟ”ใ™
  • ReflectionProperty::hasType โ€” ใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃใŒๅž‹ใ‚’ๆŒใคใ‹ใ‚’ใƒใ‚งใƒƒใ‚ฏใ™ใ‚‹
  • ReflectionProperty::isAbstract โ€” ใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃใŒ abstract ใ‹ใฉใ†ใ‹ใ‚’ๅˆคๅฎšใ™ใ‚‹
  • ReflectionProperty::isDefault โ€” ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃใงใ‚ใ‚‹ใ‹ใฉใ†ใ‹ใ‚’่ชฟในใ‚‹
  • ReflectionProperty::isDynamic โ€” ๅ‹•็š„ใชใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃใ‹ใฉใ†ใ‹ใ‚’่ชฟในใ‚‹
  • ReflectionProperty::isFinal โ€” ใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃใŒ final ใ‹ใฉใ†ใ‹ใ‚’ๅˆคๅฎšใ™ใ‚‹
  • ReflectionProperty::isInitialized โ€” ใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃใŒๅˆๆœŸๅŒ–ใ•ใ‚Œใฆใ„ใ‚‹ใ‹ใ‚’ใƒใ‚งใƒƒใ‚ฏใ™ใ‚‹
  • ReflectionProperty::isLazy โ€” ใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃใŒใƒฌใ‚คใ‚ธใƒผใงใ‚ใ‚‹ใ‹ใฉใ†ใ‹ใ‚’่ชฟในใ‚‹
  • ReflectionProperty::isPrivate โ€” private ใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃใงใ‚ใ‚‹ใ‹ใฉใ†ใ‹ใ‚’่ชฟในใ‚‹
  • ReflectionProperty::isPrivateSet โ€” ใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃใŒๆ›ธใ่พผใฟใซใคใ„ใฆ private ใ‹ใฉใ†ใ‹ใ‚’่ชฟในใ‚‹
  • ReflectionProperty::isPromoted โ€” ใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃใŒใ‚ณใƒณใ‚นใƒˆใƒฉใ‚ฏใ‚ฟใฎๅผ•ๆ•ฐใ‹ใ‚‰ๆ˜‡ๆ ผใ—ใŸใ‚‚ใฎใ‹ใ‚’่ชฟในใ‚‹
  • ReflectionProperty::isProtected โ€” protected ใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃใงใ‚ใ‚‹ใ‹ใฉใ†ใ‹ใ‚’่ชฟในใ‚‹
  • ReflectionProperty::isProtectedSet โ€” ใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃใŒๆ›ธใ่พผใฟใซใคใ„ใฆ protected ใ‹ใฉใ†ใ‹ใ‚’่ชฟในใ‚‹
  • ReflectionProperty::isPublic โ€” public ใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃใงใ‚ใ‚‹ใ‹ใฉใ†ใ‹ใ‚’่ชฟในใ‚‹
  • ReflectionProperty::isReadOnly โ€” ใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃใŒ readonly ใ‹ใ‚’่ชฟในใ‚‹
  • ReflectionProperty::isStatic โ€” staticใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃใงใ‚ใ‚‹ใ‹ใฉใ†ใ‹ใ‚’่ชฟในใ‚‹
  • ReflectionProperty::isVirtual โ€” ใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃใŒไปฎๆƒณใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃใ‹ใฉใ†ใ‹ใ‚’ๅˆคๅฎšใ™ใ‚‹
  • ReflectionProperty::setAccessible โ€” ใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃใฎใ‚ขใ‚ฏใ‚ปใ‚น็ฏ„ๅ›ฒใ‚’่จญๅฎšใ™ใ‚‹
  • ReflectionProperty::setRawValue โ€” set ใƒ•ใƒƒใ‚ฏใŒๅฎš็พฉใ•ใ‚Œใฆใ„ใŸใจใ—ใฆใ‚‚ใ€ใใ‚Œใ‚’่ฟ‚ๅ›žใ—ใฆใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃใฎๅ€คใ‚’่จญๅฎšใ™ใ‚‹
  • ReflectionProperty::setRawValueWithoutLazyInitialization โ€” ้…ๅปถๅˆๆœŸๅŒ–ใ‚’ใƒˆใƒชใ‚ฌใƒผใ›ใš็”Ÿใฎใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃๅ€คใ‚’่จญๅฎšใ™ใ‚‹
  • ReflectionProperty::setValue โ€” ใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃใฎๅ€คใ‚’่จญๅฎšใ™ใ‚‹
  • ReflectionProperty::skipLazyInitialization โ€” ใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃใ‚’้žใƒฌใ‚คใ‚ธใƒผใจใ—ใฆใƒžใƒผใ‚ฏใ™ใ‚‹
  • ReflectionProperty::__toString โ€” ๆ–‡ๅญ—ๅˆ—ใซๅค‰ๆ›ใ™ใ‚‹
๏ผ‹add a note

User Contributed Notes 1 note

up
7
rasmus at mindplay dot dk ยถ
15 years ago
I think a more accurate explanation is this:

The Reflection classes are designed to reflect upon the source code of an application, not on any runtime information.

I think you misunderstand the ReflectionProperty constructor in your example above. The fact that it accepts an object as argument is just a convenience feature - you are actually inspecting the class of that object, not the object itself, so it's basically equivalent to:

<?php

// works fine 
$Reflection = new ReflectionProperty(get_class($a), 'a');

// throws exception 
$Reflection = new ReflectionProperty(get_class($a), 'foo');

?>

Getting the class of the object you're passing in is implied, since inspecting a defined property is the purpose of this class.

In your example, $a->foo is a dynamic member - it is not defined as a member of class, so there is no defining class reference, line number, default value, etc. - which means, there is nothing to reflect upon.

Clearly this very useful library could use some real documentation...