错误" Closure对象不能具有属性"

时间:2017-06-12 09:32:25

标签: php symfony twig closures

这个函数给我一个" Closure对象不能有属性"错误:

public function activitiesSummaryAction()
    {
        $classes = array();

        for($i = 1; $i <=10; $i++)
        {
            array_push($classes, new Classroom($i, function(){

                $activites = array();

                for ($i = 1; $i <=10; $i++)
                {
                    array_push($activites, new Activity(rand(1,7), rand(2016,2018)));
                }

                return $activites;
              }
           ));
        }


        return $this->render('teacher/activitiesSummary.html.twig', [
            'classes' => $classes,
        ]);
    }

实际上我不确定PHP是否可以支持这种回调函数。谁能告诉我如何纠正这个问题或让它以另一种方式运作?

编辑:以下是课堂和活动课程:

class Activity {

    public $_id;
    public $_annee;
    public $_inProgress;

    function __construct($id, $annee) {
        $this->_id = $id;
        $this->_annee = $annee;
        $this->_inProgress = rand(0,1);
    }
}


class Classroom {
    public $_id;
    public $_activities;

    function __construct($id, $activities) {
        $this->_id = $id;
        $this->_activities = array($activities);
    }
}

1 个答案:

答案 0 :(得分:0)

您需要使用call_user_func()。 创建一个要用作回调的函数:

let value = UInt32(bigEndian: 1500)

print(String(format:"%08X", value.bigEndian)) //000005DC
print(String(format:"%08X", value.littleEndian)) //DC050000

然后你需要在构造函数中使用call_user_func():

function myCallback(){
    $activites = array();
    for ($i = 1; $i <=10; $i++)
    {
       array_push($activites, new Activity(rand(1,7), rand(2016,2018)));
    }
    return $activites;
}

最后编辑您在此处发布的代码:

class Classroom {
    public $_id;
    public $_activities;

    function __construct($id, $activities) {
      $this->_id = $id;
      $this->_activities = array(call_user_func($activities));
    }
}