略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: ReflectionClass::getProperty

2024-12-25

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 noteadd a note

User Contributed Notes 2 notes

up
32
eric at naeseth dot com
10 years ago
If the class doesn't have a property with the given name, a ReflectionException will be raised.
up
9
dohpaz42
7 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.

官方地址:https://www.php.net/manual/en/reflectionclass.getproperty.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3