Inversion of Control: The session scope
Similarly to the singleton scope there exists a session scope. Contrary to the singleton scope which keeps one instance of a class but recreates this instance on every request, the session scope keeps one instance of a class per session - the instance is only created on its first instantiation and then stored in the session. On the next request the same instance will be taken from the session.
<?php $binder = new stubBinder(); $binder->bind('Car')->to('BMW')->in(stubBindingScopes::$SESSION); // other bindings $injector = $binder->getInjector(); $bmw1 = $injector->getInstance('Car'); $bmw2 = $injector->getInstance('Car'); ?>
Now, $bwm1 and $bmw2 reference the same instance which is stored in the session. If the above code snippet is executed on the next request, both variables would contain the same instance as in the first request.
Please bear in mind that the session scope must know the session. This can be done in two ways: first by explicitly setting the session instance:
stubBindingScopes::$SESSION->setSession($mySession);
The other (and preferred) way is to use the net::stubbles::ipo::ioc::stubIpoBindingModule as explained below, which takes care of injecting the session into the scope.
