Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If your class is not meant to define some super-type, it should not be declared as <strong><code>abstract</code></strong>, I'd say.</p> <p>In your case, I would rather go with a class :</p> <ul> <li>That defines <strong><code>__construct</code></strong> and <strong><code>__clone</code></strong> as private methods <ul> <li>so the class cannot be instanciated from outside</li> </ul></li> <li>And, this way, your class could create an instance of itself <ul> <li>See the <strong><a href="http://en.wikipedia.org/wiki/Singleton_pattern" rel="noreferrer">Singleton design pattern</a></strong>, about that, btw</li> </ul></li> </ul> <p><br> Now, why use a Singleton, and not only static methods ? I suppose that, at least a couple of reasons can be valid :</p> <ul> <li>Using a singleton means using an instance of the class ; makes it easier to transform a non-singleton class to a singleton one : only have to make <code>__construct</code> and <code>__clone</code> private, and add some <code>getInstance</code> method.</li> <li>Using a singleton also means you have access to everything you can use with a normal instance : <code>$this</code>, properties, ...</li> <li>Oh, a third one <em>(not sure about that, but might have its importance)</em> : with PHP &lt; 5.3, you have less possibilities with static methods/data : <ul> <li><a href="http://fr2.php.net/__call" rel="noreferrer"><strong><code>__callStatic</code></strong></a> has only been introduced in PHP 5.3</li> <li>There is no <code>__getStatic</code>, <code>__setStatic</code>, ...</li> <li>Same for a couple of other <a href="http://fr2.php.net/manual/en/language.oop5.magic.php" rel="noreferrer"><strong>Magic methods</strong></a> !</li> </ul></li> <li><a href="http://fr2.php.net/manual/en/language.oop5.late-static-bindings.php" rel="noreferrer">Late Static Binding</a> has only been added with PHP 5.3 ; and not having it often makes it harder, when working with static methods/classes ; especially when using inheritance.</li> </ul> <p><br> This being said, yes, some code like this :</p> <pre><code>abstract class MyClass { protected static $data; public static function setA($a) { self::$data['a'] = $a; } public static function getA() { return self::$data['a']; } } MyClass::setA(20); var_dump(MyClass::getA()); </code></pre> <p>Will work... But it doesn't feel quite natural... and this is a very simple example <em>(see what I said earlier with Late Static Binding, and magic methods)</em>.</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. 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