Search code examples
phpoopstaticlate-bindingclass-variables

Get a static property of an instance


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.


Solution

  • You need to lookup the class name first:

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

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