Dynamically call a static variable (array)

后端 未结 5 1222
情深已故
情深已故 2021-01-03 07:57

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 an

5条回答
  •  醉酒成梦
    2021-01-03 08:10

    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

提交回复
热议问题