Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all properties of the current class and NOT its parent(s) in PHP

How do I get an array of all the properties of the current class, excluding inherited properties?

like image 469
badger Avatar asked Dec 08 '22 02:12

badger


1 Answers

You can reach it only with reflection, here suitable example:

<?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);

Result:

array (
  0 => 'propery2',
  1 => 'propery3',
  2 => 'propery4',
)
like image 89
cn007b Avatar answered Dec 11 '22 08:12

cn007b