<?php
interface Creator
{
public function create(): void;
}
class EchoCreator implements Creator
{
public function create(): void
{
echo 'Hello world!';
}
}
trait CreatorAble
{
abstract protected function creator(): Creator;
public function useCreator(): void
{
$this->creator()->create();
}
}
final class SomeClass
{
private $creator;
use CreatorAble;
public function __construct(Creator $creator)
{
$this->creator = $creator;
}
private function creator(): Creator
{
return $this->creator;
}
}
(new SomeClass(new EchoCreator))->useCreator();
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
Hello world!
Output for 7.0.0 - 7.0.20
Hello world!
Fatal error: Uncaught TypeError: Return value of EchoCreator::create() must be an instance of void, none returned in /in/deDEh:14
Stack trace:
#0 /in/deDEh(23): EchoCreator->create()
#1 /in/deDEh(46): SomeClass->useCreator()
#2 {main}
thrown in /in/deDEh on line 14
Process exited with code 255.