Get a static property of an instance

commonpike picture commonpike · Apr 11, 2011 · Viewed 14.1k times · Source

If I have an instance in PHP, what's the easiest way to get to a static property ('class variable') of that instance ?

This

$classvars=get_class_vars(get_class($thing));
$property=$classvars['property'];

Sound really overdone. I would expect

$thing::property

or

$thing->property

EDIT: this is an old question. There are more obvious ways to do this in newer PHP, search below.

Answer

halfdan picture halfdan · Apr 11, 2011

You need to lookup the class name first:

$class = get_class($thing);
$class::$property

$property must be defined as static and public of course.