Here's my question for today. I'm building (for fun) a simple templating engine. The basic idea is that I have a tag like this {blog:content} and I break it in a method and a action. The problem is when I want to call a static variable dynamically, I get the following error .
Parse error: parse error, expecting `','' or `';''
And the code:
$class = 'Blog';
$action = 'content';
echo $class::$template[$action];
$template is a public static variable(array) inside my class, and is the one I want to retreive.
What about get_class_vars
?
class Blog {
public static $template = array('content' => 'doodle');
}
Blog::$template['content'] = 'bubble';
$class = 'Blog';
$action = 'content';
$values = get_class_vars($class);
echo $values['template'][$action];
Will output 'bubble'
You may want to save a reference to the static array first.
class Test
{
public static $foo = array('x' => 'y');
}
$class = 'Test';
$action = 'x';
$arr = &$class::$foo;
echo $arr[$action];
Sorry for all the editing ...
EDIT
echo $class::$foo[$action];
Seems to work just fine in PHP 5.3. Ahh, "Dynamic access to static methods is now possible" was added in PHP 5.3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With