- 实现了动态实例化与命名参数的测试用例 - 验证动态方法调用与参数展开的功能 - 测试控制器类的构造函数命名参数传递 - 验证动态方法调用返回结果的正确性 - 添加了多场景动态调用的预期输出验证pull/1/head
parent
011cb1ed8c
commit
5129bab900
1 changed files with 45 additions and 0 deletions
@ -0,0 +1,45 @@ |
||||
--TEST-- |
||||
Dynamic instantiation with named arguments and dynamic method call |
||||
--FILE-- |
||||
<?php |
||||
declare(strict_types=1); |
||||
|
||||
class HelloController { |
||||
public string $prefix; |
||||
|
||||
public function __construct(string $prefix = 'Hello') { |
||||
$this->prefix = $prefix; |
||||
} |
||||
|
||||
public function greet(string $name, int $times): string { |
||||
$result = ''; |
||||
for ($i = 0; $i < $times; $i++) { |
||||
$result .= "{$this->prefix} {$name}!\n"; |
||||
} |
||||
return $result; |
||||
} |
||||
|
||||
public function farewell(string $name): string { |
||||
return "{$this->prefix} Goodbye {$name}!"; |
||||
} |
||||
} |
||||
|
||||
function main(): void { |
||||
$handler = [HelloController::class, 'greet']; |
||||
$params = ['World', 2]; |
||||
|
||||
[$class, $method] = $handler; |
||||
// Dynamic instantiation with named args + dynamic method call with unpacking |
||||
$result = (new $class(prefix: 'Hi'))->$method(name: $params[0], times: $params[1]); |
||||
echo $result; |
||||
|
||||
// Another dynamic method call: farewell |
||||
$handler[1] = 'farewell'; |
||||
[$class, $method] = $handler; |
||||
echo (new $class(prefix: 'Ohayo'))->$method(name: 'AOT'); |
||||
} |
||||
?> |
||||
--EXPECT-- |
||||
Hi World! |
||||
Hi World! |
||||
Ohayo Goodbye AOT! |
||||
Loading…
Reference in new issue