class TestCallEcho{
protected $called = [];
public function __call($name, $arguments)
{
$this->called[] = [$name, $arguments];
return $this;
}
public function end(){
$this->called[] = "end";
return $this;
}
public function getCalled(){
return $this->called;
}
}
function testArrayEq($array1, $array2){
if(count($array1) !== count($array2)){
return false;
}
foreach ($array1 as $index => $value1){
if(!isset($array2[$index])){
return false;
}
$value2 = $array2[$index];
if(is_array($value1) && is_array($value2)){
if(!testArrayEq($value1, $value2)){
return false;
}else{
//继续判断
}
}else{
if($value1 !== $value2){
return false;
}
}
}
return true;
}
function testTestArrayEq(){
$array1 = [1, 2];
$array2 = [1, 3];
$array3 = [1, 2, 3];
assert(testArrayEq($array1, $array2) == false);
assert(testArrayEq($array1, $array3) == false);
assert(testArrayEq($array1, $array1) == true);
}
testTestArrayEq();
$obj = new \stdClass();
$callEcho = new CallEcho();
/*************重点开始****************/
/** @var CallEcho $callEcho */
$callEcho = $callEcho->testNumber(1)->testString("myname")->testObj($obj)->testMulti(1, "myname")->testMulti2("1", $obj)->end();
/** @var TestCallEcho $testCallEcho */
$testCallEcho = $callEcho->invoke(new TestCallEcho());
/************重点结束****************/
//基本上和这个作用一样
$a = function($obj){
$obj->testNumber(1)->testString("myname")->testObj($obj)->testMulti(1, "myname")->testMulti2("1", $obj)->end();
};
$called = $testCallEcho->getCalled();
$eq = testArrayEq($called, [
["testNumber", [1]],
["testString", ["myname"]],
["testObj", [$obj]],
["testMulti", [1, "myname"]],
["testMulti2", ["1", $obj]],
"end"
]);
assert($eq);
|