Application 2017-10-01

Learning Design Patterns with PHP - Singleton: Limiting Instances

Understand the Singleton pattern to control instance creation, restrict access, and manage single object state in PHP.

Read in: ja
Learning Design Patterns with PHP - Singleton: Limiting Instances

What is the Singleton Pattern?

This pattern ensures that only one instance exists to control the cost of instance creation.

Structure

SingletonClass

Simply prepare a private constructor, a static method that returns only one instance, and a static variable to hold its own instance.

Advantages

Control Access to Instances

Since access to itself held by the Singleton pattern is restricted to private, you can control access from client-side code.

Ability to Change the Number of Instances

It is also possible to change the number of generated instances to two or more.

Disadvantages

When to Use

Implementation Example (β€»Repository available on github.)

<?php
class SingletonConfig {
    private $config;

    /**
     * a single variable
     */
    private static $instance;

    private function __construct()
    {
        $this->config = 'AUTO';
    }

    /**
     * Create a only instance
     */
    public static function getInstance()
    {
        if (!isset(self::$instance)) {
            self::$instance = new SingletonConfig();
        }

        return self::$instance;
    }

    public function getConfig()
    {
        return $this->config;
    }

    public final function __clone()
    {
        throw new RuntimeException('Clone is not allowed against' . get_class($this));
    }
}
<?php
require_once 'SingletonConfig.php';

$instanceA = SingletonConfig::getInstance();

$instanceB = SingletonConfig::getInstance();

if ($instanceA->getConfig() === $instanceB->getConfig()) {
    echo 'True';
} // true

Summary

Related Keywords

Tags: Design Pattern PHP Singleton Pattern
Share: 𝕏 Post Facebook Hatena
✏️ View source / Discuss on GitHub
β˜• Support

If you enjoy this blog, consider supporting it. Every bit helps keep it running!


Related Articles