Jump to content

Retrieve static properties dynamically


SiC_Goat

Recommended Posts

the only way i can think of uses eval:

<?php
Class MyClass
{
   public static $MyProperty = 'MyValue';
}

$className = 'MyClass';
print eval('$classProp = '.$className.'::$MyProperty;');
print $classProp;
?>

 

you could write a static function to return the value:

<?php
Class MyClass
{
   public static $MyProperty = 'MyValue';
   public static function getProp ( ) {
    return self::$MyProperty;
   }
}

$className = 'MyClass';
$classProp = call_user_func(array($className,'getProp'));
print $classProp;
?>

Interesting idea, didn't know you could use call_user_func on static portions of an object that way. I'll probably just write a static property method that takes in a $name and return self::$name or something. Better than any non-solution I have so far.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.