gpt4 book ai didi

php - 如何在 PHP 中获取当前类的所有属性而不是其父类

转载 作者:可可西里 更新时间:2023-11-01 00:08:41 24 4
gpt4 key购买 nike

如何获取当前类的所有属性的数组,不包括继承的属性?

最佳答案

你只能通过反射来达到它,这里是合适的例子:

<?php

class foo
{
protected $propery1;
}

class boo extends foo
{
private $propery2;
protected $propery3;
public $propery4;
}

$reflect = new ReflectionClass('boo');
$props = $reflect->getProperties();
$ownProps = [];
foreach ($props as $prop) {
if ($prop->class === 'boo') {
$ownProps[] = $prop->getName();
}
}

var_export($ownProps);

结果:

array (
0 => 'propery2',
1 => 'propery3',
2 => 'propery4',
)

关于php - 如何在 PHP 中获取当前类的所有属性而不是其父类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31848998/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com