How to find all non static class attributes
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?
This code was the solution for me.
$ReflectionClass = new \ReflectionClass(get_called_class());
$staticAttributes = $ReflectionClass->getStaticProperties();
$allAttributes = $ReflectionClass->getProperties();
$attributes = array_diff($staticAttributes, $allAttributes);
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()));
精彩评论