Design Patterns in PHP 8: alternative implementations

In the previous article, I showed examples of implementing design patterns Singleton and Multiton using inheritance. This is a fairly convenient approach, provided that we have several simple classes in the project that implement literally several meth…


This content originally appeared on DEV Community and was authored by Max Zhuk

In the previous article, I showed examples of implementing design patterns Singleton and Multiton using inheritance. This is a fairly convenient approach, provided that we have several simple classes in the project that implement literally several methods and require a single instance in all places where these methods are called.

The php language only allows single inheritance, and this may be the reason why the variant from the last article will complicate or even break the architecture of our application. Therefore, I propose to consider other options for implementing the patterns.

Often the correct solution is to implement the template in every class that requires it.

class Database
{
    private static self|null $instance = null;

    final private function __construct(){}
    final private function __clone(){}
    final private function __wakeup(){}

    public static function getInstance(): self
    {
        if (self::$instance === null) {
            self::$instance = new self;
        }

        return self::$instance;
    }

    public function connect()
    {
        // ...
    }
}

$db = Database::getInstance();
$db->connect();

An alternative would be to use traits. This option is suitable if the project has several classes and you want to take out part of the repeating code in order to comply with the DRY principle.

trait MultitonTrait
{
    private static array|null $instance = null;

    public static function getInstance(int|string $key): self
    {
        if (!array_key_exists($key, self::$instance)) {
            self::$instance[$key] = new self;
        }

        return self::$instance[$key];
    }
}

class Database
{
    use MultitonTrait;

    final private function __construct(){}
    final private function __clone(){}
    final private function __wakeup(){}

    public function connect()
    {
        // ...
    }
}

$db = Database::getInstance('mysql');
$db->connect();

All options from this article and the previous one have both their pros and cons, so the best solution should be chosen based on the architecture of a particular project.


This content originally appeared on DEV Community and was authored by Max Zhuk


Print Share Comment Cite Upload Translate Updates
APA

Max Zhuk | Sciencx (2022-07-22T17:16:32+00:00) Design Patterns in PHP 8: alternative implementations. Retrieved from https://www.scien.cx/2022/07/22/design-patterns-in-php-8-alternative-implementations/

MLA
" » Design Patterns in PHP 8: alternative implementations." Max Zhuk | Sciencx - Friday July 22, 2022, https://www.scien.cx/2022/07/22/design-patterns-in-php-8-alternative-implementations/
HARVARD
Max Zhuk | Sciencx Friday July 22, 2022 » Design Patterns in PHP 8: alternative implementations., viewed ,<https://www.scien.cx/2022/07/22/design-patterns-in-php-8-alternative-implementations/>
VANCOUVER
Max Zhuk | Sciencx - » Design Patterns in PHP 8: alternative implementations. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/07/22/design-patterns-in-php-8-alternative-implementations/
CHICAGO
" » Design Patterns in PHP 8: alternative implementations." Max Zhuk | Sciencx - Accessed . https://www.scien.cx/2022/07/22/design-patterns-in-php-8-alternative-implementations/
IEEE
" » Design Patterns in PHP 8: alternative implementations." Max Zhuk | Sciencx [Online]. Available: https://www.scien.cx/2022/07/22/design-patterns-in-php-8-alternative-implementations/. [Accessed: ]
rf:citation
» Design Patterns in PHP 8: alternative implementations | Max Zhuk | Sciencx | https://www.scien.cx/2022/07/22/design-patterns-in-php-8-alternative-implementations/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.