Converting code with Anonymous functions to PHP 5.2

前端 未结 2 1742
醉酒成梦
醉酒成梦 2020-12-19 00:32

I have some PHP 5.3 code which builds an array to be passed to a view. This is the code I have.

# Select all this users links.
$data = $this->link_model-&         


        
2条回答
  •  情书的邮戳
    2020-12-19 01:30

    You could call one of those function like so:

    $func = $callbacks[0];
    $func();
    

    Which also works with create_function() and using strings for named functions like so:

    function test() {
      echo "test";
    }
    $func = 'test';
    $func();
    
    $func = create_function('' , 'echo "test 2"; ');
    $func();
    

    Also, if the calling is done using call_user_func you can use array($object, 'func_name') to call a public method on an object or a class with array('Class_Name', 'func_name'):

    class Test {
      public static function staticTest() { echo "Static Test"; }
      public function methodTest() { echo "Test"; }
    
      public function runTests() {
        $test = array('Test', 'staticTest');
        call_user_func($test);
    
        $test = array($this, 'methodTest');
        call_user_func($test);
      }
    }
    
    $test = new Test();
    $test->runTests();
    

提交回复
热议问题