1

我有以下代码并试图找到解决方案:

<?php

class t1 {

    public $response = array();
    public $request = array();

    public function getRequest() {
        return $this->request;
    }

    public function getResponse() {
        return $this->response;
    }

}

class t2 extends t1 {
    public function run($f) {
        $this->response = $f($this);
        return $this;
    }
}

$delegate = function($c)
{
    // PLACEHOLDER
    // This is the only place to update test code
    // It's not allowed to modify any other section of this code
};

$t = new t2();
print_r(array("request" => $t->run($delegate)->getRequest(), "response" => $t->getResponse()));
?>

我假设 $delegate 是一个动态函数。任何人都可以带我完成这个。

我在想 PLACEHOLDER 应该是:

4

1 回答 1

0

我假设 $delegate 是一个动态函数。

是的。$delagate是一个 PHP闭包

如果您将函数定义为:

$delegate = function($c)
{

};

并将其传递给$t->run,然后$c将是$t实例。

于 2013-10-07T19:34:04.403 回答