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 __construct(object|string $class, string $property)
private __clone(): void
public static export(mixed $class, string $name, bool $return = ?): string
public getAttributes(?string $name = null, int $flags = 0): array
public getHook(PropertyHookType $type): ?ReflectionMethod
public getHooks(): array
public getModifiers(): int
public getName(): string
public getRawValue(object $object): mixed
public getValue(?object $object = null): mixed
public hasHook(PropertyHookType $type): bool
public hasHooks(): bool
public hasType(): bool
public isAbstract(): bool
public isDefault(): bool
public isDynamic(): bool
public isFinal(): bool
public isInitialized(?object $object = null): bool
public isLazy(object $object): bool
public isPrivate(): bool
public isPrivateSet(): bool
public isPromoted(): bool
public isProtected(): bool
public isPublic(): bool
public isReadOnly(): bool
public isStatic(): bool
public isVirtual(): bool
public setAccessible(bool $accessible): void
public setRawValue(object $object, mixed $value): void
public setValue(?object $object, mixed $value): void
public __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 版本间发生更改。建议始终使用常量而不直接依赖值。

目录

添加备注

用户贡献的备注 2 notes

up
7
rasmus at mindplay dot dk
14 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...
up
0
mitgath at gmail dot com
19 days ago
There is way to get property type without reflection.
Not very clear, but there is:

<?php

function getPropType(object $object, string $propName): string
{
try {
$object->{$propName} = new class {};
} catch (
\TypeError $e) {
// @hint: question mark in type can be used to detect if property is nullable
if (preg_match('/of type \??(.*)$/', $e->getMessage(), $m)) {
return
$m[1];
}
throw
$e;
} catch (
\Error $e) {
// property doesn't exist or is not public
throw $e;
}
throw new
\TypeError(sprintf('Property %s of class %s has no type', $propName, $object::class));
// alternatively return 'mixed';
}
?>

note: in case you need access to private/protected properties its easy to change this to be embedded in any class:

<?php

trait PropertyTypeResolver
{
public function
getPropType(string $propName): string
{
try {
$this->{$propName} = new class {};
} catch (
\TypeError $e) {
// @hint: question mark in type can be used to detect if property is nullable
if (preg_match('/of type \??(.*)$/', $e->getMessage(), $m)) {
return
$m[1];
}
throw
$e;
} catch (
\Error $e) {
// property doesn't exist
throw $e;
}
throw new
\TypeError(sprintf('Property %s of class %s has no type', $propName, $this::class));
// alternatively return 'mixed';
}
}
?>
To Top