Function literal in PHP class

后端 未结 3 1880
挽巷
挽巷 2020-12-21 08:20

Take a look at this code, please:

$array = array(
    \'action\' => function () { echo \"this works\"; }
);

class Test {
    public $array = array(
              


        
3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-21 09:08

    I dont have chance to test Your code on PHP 5.6.6, but I think this code resolve Your problem.

    class Test{
    
        public $array;
    
        function __construct(){
    
                $this -> array = array(
    
                    'action'    =>  function (){
    
                        echo 'It works too';
                    }
                );
        }
    }
    
    $test = new Test();
    $test -> array['action']();
    

提交回复
热议问题