Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>After some playing around the best method I can think of is like so:</p> <p>Create a file called SingletonBase.php and include it in root of your script!</p> <p>The code is</p> <pre><code>abstract class SingletonBase { private static $storage = array(); public static function Singleton($class) { if(in_array($class,self::$storage)) { return self::$storage[$class]; } return self::$storage[$class] = new $class(); } public static function storage() { return self::$storage; } } </code></pre> <p>Then for any class you want to make a singleton just add this small single method.</p> <pre><code>public static function Singleton() { return SingletonBase::Singleton(get_class()); } </code></pre> <hr> <p>Here is a small example:</p> <pre><code>include 'libraries/SingletonBase.resource.php'; class Database { //Add that singleton function. public static function Singleton() { return SingletonBase::Singleton(get_class()); } public function run() { echo 'running...'; } } $Database = Database::Singleton(); $Database-&gt;run(); </code></pre> <p>And you can just add this singleton function in any class you have and it will only create 1 instance per class.</p> <p>Just another idea you can also do</p> <pre><code>if(class_exists('Database')) { $Database = SingletonBase::Singlton('Database'); } </code></pre> <hr> <p>and at the end of your script you can do some dfebugging if you need too,</p> <p>at the end of your script you can just do</p> <pre><code>foreach(SingletonBase::storage () as $name =&gt; $object) { if(method_exists("debugInfo",$object)) { debug_object($name,$object,$object-&gt;debugInfo()); } } </code></pre> <p>so this method will be great for a debugger to get access to all classes and object states that have been initialized </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. 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