Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>class Application { protected static $_singletonFoo=NULL; public static function foo() { if(NULL === self::$_singletonFoo) { self::$_singletonFoo = new Foo; } return self::$_singletonFoo; } } </code></pre> <p>This is the way I'd do it. It creates the object on demand:</p> <pre><code>Application::foo()-&gt;bar(); </code></pre> <p>It's the way I am doing it, it respects OOP principles, it's less code than how you're doing it right now,and the object is created only when the code needs it for the first time.</p> <p><strong>Note</strong>: what I've presented is not even a real singleton pattern. A singleton would allow only one instance of itself by defining the constructor (Foo::__constructor()) as private. It is only a "global" variable available to all "Application" instances. That's why I think its use is valid as it does NOT disregard good OOP principles. Of course, as anything in the world, this "pattern" should not be overused either!</p> <p>I've seen this being used in many PHP frameworks, Zend Framework and Yii among them. And you should use a framework. I'm not going to tell you which one.</p> <p><strong>Addendum</strong> For the ones among you worrying about <a href="http://en.wikipedia.org/wiki/Test-driven_development" rel="nofollow noreferrer">TDD</a>, you can still make up some wiring to dependency-inject it. It could look like this:</p> <pre><code>class Application { protected static $_singletonFoo=NULL; protected static $_helperName = 'Foo'; public static function setDefaultHelperName($helperName='Foo') { if(is_string($helperName)) { self::$_helperName = $helperName; } elseif(is_object($helperName)) { self::$_singletonFoo = $helperName; } else { return FALSE; } return TRUE; } public static function foo() { if(NULL === self::$_singletonFoo) { self::$_singletonFoo = new self::$_helperName; } return self::$_singletonFoo; } } </code></pre> <p>There's enough room for improvement. It's just a PoC, use your imagination.</p> <p>Why do it like that? Well, most of the time the application won't be unit-tested, it will actually be run, <em>hopefully in a production environment</em>. The strength of PHP is its speed. PHP is NOT and never will be a "clean OOP language", like Java. </p> <p>Within an application, there is only one Application class and only one instance of each of its helpers, at most (as per lazy loading as above). Sure, <strong>singletons are bad, but then again, only if they don't adhere to the real world.</strong> In my example, they do.</p> <p>Stereotyped "rules" like "singletons are bad" are the source of evil, they're for lazy people not willing to think for themselves.</p> <p>Yeah, I know, the PHP manifesto is BAD, technically speaking. Yet it's a successful language, in its hackish way.</p> <h2>Addendum</h2> <p>One function style:</p> <pre><code>function app($class) { static $refs = array(); //&gt; Dependency injection in case of unit test if (is_object($class)) { $refs[get_class($class)] = $class; $class = get_class($class); } if (!isset($refs[$class])) $refs[$class] = new $class(); return $refs[$class]; } //&gt; usage: app('Logger')-&gt;doWhatever(); </code></pre>
    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