A sort of 'global' PHP class
<p>In my web application, my database connection adapter is being created as one of the first things. Prior to that, the application configuration is built. Both are instantiated through their appropriate Zend components from ZF2. I note that I am only using ZF2 as a library, and not as a framework per se.</p>

<p>Basically, this is what's happening in my application initialisation.</p>

<pre><code>$config   = new \Zend\Config\Config(array());
$database = new \Zend\Db\Adapter\Adapter($config-&gt;database-&gt;toArray);

/**
*Create the session, start the session
*Set error handling
*/
</code></pre>

<p>Cool, so that's the bulk of it. Now, we then deal with the request:</p>

<pre><code>$router = new \ils\Router($_SERVER['REQUEST_URI'], $database]);
$router-&gt;dispatch();
</code></pre>

<p>Inside my router, I'm basically splitting up the URI into its components, checking that the route is valid then dispatching it using:</p>

<pre><code>$dispatchedController = new $this-&gt;controller($this-&gt;database);
$method = $this-&gt;method;
</code></pre>

<p>I am basically injecting the database component, which I provided from the initialisation into the controller through the router. Is that close enough to DI? How can I improve it?</p>

<p>So inside my controller class, I have the contractor</p>

<pre><code>public function __construct($database){
    $this-&gt;database = $database;
}
</code></pre>

<p>That's that. I can then pass it onto the relevant DAO as required etc.</p>

<p>My problem, is that in some places in the application, particularly views and some other classes, I've got <strong><em>services</em></strong> - probably not the type that you're thinking of! These services only relay information, and usually are not instantiated as objects:</p>

<pre><code>class UserService {

  public static function getUsername(){

   $container = new \Zend\Session\Container('application_session');

   return $sessionContainer-&gt;userId;

  } 
</code></pre>

<p>And then we have a method which looks like this - and this is the problem:</p>

<pre><code>public function getCurrentUser(){

    $user = new \ils\objects\user();
    $user-&gt;setUserId(self::currentUserId());

    $data = new \model\dao\users($database);
    $user = $data-&gt;loadUser($user);

    return $user;
}
</code></pre>

<p>Now, where do I get <code>$database</code> from? Whilst I could in some places instantiate the class service as an object, then feed off it, there are a few places where I can't. </p>

<p>I was thinking of possibly using Zend's <code>ServiceManager</code> to hook things into it - but how would I get the ServiceManager?</p>

<p>Potentially, one could use Singletons to create a sort of global container of standard objects (config, database etc.)?</p>
