Function literal in PHP class

后端 未结 3 1884
挽巷
挽巷 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 08:59

    Try it like this, let me know if this works for you

     function () { echo "this works"; });
    class Test {
        public $arr;
        function __construct() {
            $this->arr = array("action" => function () { echo "this works too"; });
        }
        function getArr(){
            var_dump($this->arr);
        }
    }
    
    var_dump($array);
    $obj = new Test();
    $obj->getArr();
    

提交回复
热议问题