Write a wrapper class for working with cookie . The class must contain the following set of functions:

  • preservation
  • deletion
  • editing
  • reading

By default, the cookie should be set to 1 year. The class must be implemented in such a way that it is impossible to create more than one instance of the class.

 class Cookie { public static function instance() { static $instance = false; if( $instance === false ) { $instance = new static(); } return $instance; } public static function setCookie($key, $value, $time = 31536000) { setcookie($key, $value, time() + $time, '/') ; } public static function getCookie($key) { if ( isset($_COOKIE[$key]) ){ return $_COOKIE[$key]; } return null; } public static function updateCookie($key, $value, $time = 31536000) { if ( isset($_COOKIE[$key]) ){ self::deleteCookie($key); setcookie($key, $value, time() + $time, '/'); } return null; } public static function deleteCookie($key) { if ( isset($_COOKIE[$key]) ){ self::getCookie($key); unset($_COOKIE[$key]); } } 

}

that's what i did and i can't figure out if i

  • Homework must be done independently. If you have a question about homework, do not ask him to do it for you. Ask a specific question about a problem that you cannot solve. - Yaroslav Molchan
  • suppose if so - Vasily Pupkin

2 answers 2

pattern singleton

Singleton is characterized by two features:

  • Public static method to get a class instance
  • All methods that allow you to create a copy of an instance (including the constructor) are private

The simplest snippet for a singleton looks like this:

 class A { private static $a; public static function get() { return static::$a?: static::$a = new static(); } private function __construct(){} private function __clone(){} private function __wakeup(){} } 

    I will show you on a simple example of the singloton pattern:

     class Cookie { public static function instance() { static $instance = false; if( $instance === false ) { // Позднее статическое связывание (PHP 5.3+) $instance = new static(); } return $instance; } /** * Make constructor private, so nobody can call "new Class". */ private function __construct() {} /** * Make clone magic method private, so nobody can clone instance. */ private function __clone() {} /** * Make sleep magic method private, so nobody can serialize instance. */ private function __sleep() {} /** * Make wakeup magic method private, so nobody can unserialize instance. */ private function __wakeup() {} //Добавляете вашу логику } 

    And then you can work with this object:

     $cookie = Cookie::instance(); $cookie->setCookie('name', 'value'); $value = $cookie->getCookie('name'); ... 
    • corrected? so it will look like? - Vasily Pupkin
    • Must, check and then tell me if it works or not - Yaroslav Molchan
    • work then works) thanks, the problem is that until the end I didn’t understand if "our" is fulfilling: D implementation of the conditions of the assignment - Vasily Pupkin
    • @ VasilyPupkin yes, this is Singleton :) - Yaroslav Molchan