Implementation for a singleton pattern
composer require ap-lib/singleton
- PHP 8.1 or higher
use AP\Singleton\Singleton;
class Hello
{
use Singleton;
private string $format;
public function sayHello(string $name): string
{
return sprintf($this->format, $name);
}
private function __construct()
{
$this->format = "Hello %s";
}
}
echo Hello::getInstance()->sayHello("World"); // Hello World