class Cookie{ public static int $baseTime = 86400 * 7; public static string $basePath = '/'; protected static $instance; //set protected for our constructor. This will not allow instances of our class to be created! protected function __construct(){} // write a method that will create an instance of our class // before creating a new object, check whether another class object has already been created public static function getInstance() : self{ if(self::$instance === null){ self::$instance = new self(); } return self::$instance; } public function get(string $name) : ?string{ return $_COOKIE[$name] ?? null; }
The classic singleton implementation. PHP example
Approved. Code works!
This is exactly the working code that is verified by the moderator or site administrators
More
lil-code© | 2022 - 2024
Go Top