Php patterns: singleton, design patterns: singleton in php

The Singleton kiến thiết pattern is a creational kiến thiết pattern where a classensures it has only one instance with lazy initialization, & can be accessedfrom a global scope. It has encapsulated “just-in-time initialization” or“initialization on first use”.

Bạn đang xem: Php patterns: singleton, design patterns: singleton in php

To solve this, we could use global variables (constants) inside a class.However, this doesn’t make classes modular và can be used only for the currentapplication implementation. Therefore, this is considered a bad practice.Another approach to solve sầu this would be lớn use the singleton pattern.


obatambeienwasirherbal.comhường. example of a Singleton class:

class Singleton /**
var Singleton Reference to lớn singleton class instance. */ private static $instance; /** * Private constructor ensures the class can be initialized only from * itself. */ private function __construct() /** * Get a singleton class instance with lazy initialization only on first * Gọi. Client code can therefore use only this accessor method khổng lồ * manipulate the singleton. * *
return Singleton */ public static function getInstance() if (!isset(self::$instance)) self::$instance = new self(); return self::$instance; /** *
throws Exception to prevent cloning object. */ public function __clone() throw new Exception("You cannot clone singleton object");
The singleton pattern can be extended khổng lồ support access khổng lồ anapplication-specific number of instances.

Inheritance of the singleton class is not possible when using a staticaccessor method. Also, deleting an instance of a singleton class is anon-trivial design problem.

When lớn use the singleton pattern?

A singleton should be considered only if all of the following criteria are met:

Ownership of the single instance cannot be reasonably assigned. Lazy initialization is desirable. Global access is not otherwise provided (in case of legacy applications).

Xem thêm: Lỗi Executenonquery Là Gì ? Phần 4: Lập Trình Cơ Sở Dữ Liệu Với C#

If the above criteria does not present implementation issues in the applicationcode, then in most cases, the dependency injectionshould be used for better testability and flexible maintainability.

When accessing the global scope, the advantage of the singleton pattern overglobal variables is that it ensures the number of instances which can bechanged to any required number any time.

The same as using global variables inside classes, the singleton patternimplemented on class level is considered an anti-pattern because it reducestestability and maintainability of the code. To replace global variables with asingleton is a wrong approach to solve sầu access of global scope from a class.

A singleton can be used when it is simpler to lớn pass an object resource as areference to the objects that need it instead of letting objects access theresource globally. It might be useful & handy to use a singleton, but theappropriate visibility of an object must be thought through.

The singleton pattern implemented on factory, incubator, or service containermàn chơi is not an anti-pattern. Abstract factory, builder, & prototype can usea singleton in their implementation. Facade & state objects are also oftemãng cầu singleton, because only one class instance is required.