tags:

views:

44

answers:

3

I want to get all non static class vars from a class. The problem I have is in the "Non static" part. The following:

foreach(array_keys(get_class_vars(get_called_class())) AS $key) {
    echo $key;
}

How can I find out $key is a static property or a non static. I know I could try something like:

@$this->$key

But there must be a better way to check this.

Anybody?

+1  A: 

Thanks to Stereofrog who pointed me at the reflectionClass.

This code was the solution for me.

$ReflectionClass = new \ReflectionClass(get_called_class());

$staticAttributes = $ReflectionClass->getStaticProperties();
$allAttributes = $ReflectionClass->getProperties();

$attributes = array_diff($staticAttributes, $allAttributes);
Stegeman
A: 

Bas van Dorst
A: 
class testClass
{
    private static $staticValPrivate;

    protected static $staticValProtected;

    public static $staticValPublic;

    private $valPrivate;

    protected $valProtected;

    public $valPublic;

    public function getClassProperties()
    {
        return get_class_vars(__CLASS__);
    }

    public function getAllProperties()
    {
        return get_object_vars($this);
    }

}

$x = new testClass();

var_dump($x->getClassProperties());
echo '<br />';

var_dump($x->getAllProperties());
echo '<br />';

var_dump(array_diff_key($x->getClassProperties(),$x->getAllProperties()));
Mark Baker