Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A singleton object is an object that is only instantiated once. That is not the same as the <a href="http://c2.com/cgi/wiki?SingletonPattern" rel="noreferrer"><em>Singleton Pattern</em></a>, which is a (Anti-) Pattern how to write a class that <em>can</em> be only instantiated once, the <em>Singleton</em> (large <em>S</em> at the beginning):</p> <blockquote> <p>“Ensure a class has only one instance, and provide a global point of access to it.” </p> </blockquote> <p>As far as PHP is concerned, you normally do not need to implement the Singleton Pattern. In fact you should avoid to do that when you ask for <em>best practice</em>, because it is <em>bad practice</em>.</p> <p>Additionally most PHP code examples you find are half-ready implementations of the pattern that neglect how PHP works. These bogus implementations do not go conform with the "ensure" in the pattern.</p> <p>This also tells something: Often that it is not needed. If a sloppy implementation does the work already while not even coming close to what the pattern is for, the wrong pattern has been used for the situation, it's starting to become an <em>Anti-Pattern</em>.</p> <p>In PHP there normally is no need to ensure at all costs that a class has only one instance, PHP applications are not that complex that you would need that (e.g. there are no multiple threads which might need to refer to an atomic instance). </p> <p>What is often left is the global access point to the class instance, which is for what most PHP developers (mis-) use the pattern. As it's known as-of today, using such "Singletons" lead to the standard problems of global static state that introduce complexity into your code across multiple levels and decrease re-usability. As a programmer you loose the ability to use your code in a flexible way. But flexbility is a very important technique to solve problems. And programmers are solving problems the whole day long.</p> <p>So before applying a <em>Design Pattern</em> the pro and cons need to be evaluated. Just using some pattern most often is not helpful.</p> <p>For starters I would say, just write your classes and take care how and when they are instantiated in some other part of your application logic so things are kept flexible.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload