Laravel Classroom image

Prashant  Nigam / Student / Web Technology

 
To post your Question Join Classroom
 
Lesson Topics's No:-  First|1 | 2 | 3|
Lessons:-The IoC Container

20.3 Singletons
Resolving our discoball is useful, but what if our discoball was expensive on resources to
instantiate, or should only be instantiated once? The register method will not be useful in this
case, since the closure is executed with every call to resolve() and a new instance of the object
is returned each time. This is where the singleton design pattern comes in.
The singleton design pattern involves writing your classes in a certain way, so that they can be
called using a static method, and will always return the same instance of itself. This way the
class is instantiated only once.For more information on the Singleton design pattern, I would suggest a quick Google search,
or check out the PHP API which has an article on the subject.
Singletons can be useful, but they require a certain class structure to be able to use them. The
IoC container has a singleton() method which makes the process a lot more simple, and does
not require any special kind of class. Let’s register our discoball as a singleton instead..
1 <?php
2 // application/start.php
3
4 IoC::singleton('discoball', function() {
5
6 // instantiate our object as before
7 $db = new Discoball(Discoball::SHINY);
8 $db->configure_shinyness('max');
9 $db->spin_speed('8900rpm');
10
11 // hand the object as the result of the closure
12 return $db;
13 });
As you can see, the process is almost identical to registering an object, except that we use the
method singleton() which accepts the same parameters.
When we resolve our discoball, the closure will only be run the first time resolve() is called,
the resulting object will be stored, and any future calls to the resolve() method will return the
same object instance. For example..
1 <?php
2
3 // closure is executed, and a discoball
4 // instance is returned
5 $db = IoC::resolve('discoball');
6
7 // the same instance of the discoball
8 // in the above statement is returned
9 $another = IoC::resolve('discoball');
Great! It’s also worth noting that you can pass an already instantiated object as a second
parameter to the singleton() method, and it will be returned by all future requests to resolve()
for example..
1 <?php
2
3 $db = new Discoball(Discoball::SHINY);
4 IoC::singleton('discoball', $db);5
6 // get hold of our discoball
7 $ball = IoC::resolve('discoball');
In a future chapter we will discuss using the IoC container and dependency injection in
combination with unit testing.
Thanks to Andrew Smith and Julien Tant (AoSix) for buying the book. Julien had the following
to say..
Hey Dayle, thanks a lot for this book, the Laravel framework is just awesome ! If
any french guys read this, visit http://www.laravel.fr/ !
Thanks guys, and great work translating the tutorials to French!
Also thanks to Proger_XP for buying the book and translating my tutorials to Russian, which
are available at Laravel.ru. Great work!

 

 

 

 
 
 

Prashant  Nigam

Skills    Laravel

Qualifications :-
Location :-,,,
Description:-
Explore
 

  Students (0)