TypePHP 编译器
https://swoole.com/aot/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
28 lines
573 B
28 lines
573 B
--TEST--
|
|
Symfony pattern: dynamic class instantiation with null coalesce default
|
|
--FILE--
|
|
<?php
|
|
|
|
class SymfonyLikeNamedService
|
|
{
|
|
public function __construct(public string $name = 'default')
|
|
{
|
|
}
|
|
}
|
|
|
|
function createService(?string $class, ?string $name = null): object
|
|
{
|
|
$class ??= SymfonyLikeNamedService::class;
|
|
|
|
return new $class($name ?? 'fallback');
|
|
}
|
|
|
|
function main(): void
|
|
{
|
|
var_dump(createService(null)->name);
|
|
var_dump(createService(SymfonyLikeNamedService::class, 'custom')->name);
|
|
}
|
|
?>
|
|
--EXPECT--
|
|
string(8) "fallback"
|
|
string(6) "custom"
|
|
|