PHP code example of codememory / singleton

1. Go to this page and download the library: Download codememory/singleton library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

codememory / singleton example snippets




use Codememory\Patterns\Singleton\SingletonTrait;

использовать singleton
class ClassWithSingleton
{

    // Подключаем singleton и можно использовать
    use SingletonTrait;
    
    private int $number = 0;
    
    public function setNumber(int $number): void
    {
    
        $this->number = $number;
    
    }
    
    public function getNumber(): int
    {
    
        return $this->number;
    
    }

}

ClassWithSingleton::getInstance()->setNumber(10);

echo ClassWithSingleton::getInstance()->getNumber(); // 10