so in the last episode we saw that laravel start from this index.PHP file so
the first thing in this index.PHP is requiring this autoload that's okay but
this app.PHP is very important so let's open that which is inside this
bootstrap app dot PHP okay so here the first important thing is new above this
class so let's open this class and this is the entire level application that's
why in the name is application dot PHP and if you go at the top here you can
see this class actually extends container and that exactly what we need
to learn in this episode so let's open this class and before checking out this
class and what this class have let me show you our implementation of container
class so I have this where that yeah I have this index to achieve which is just
a plain file so let's create a PHP tag and here I will say class then container
and here I will say protected bindings and this is just an empty array okay
then create a function called bind and this bind function except two parameters
one is the name and other is a Colleyville so call label and we call it
resolver then what we are going to say this dot bindings actually we are
talking about this variable this dot binding and the key is actually neem and
its value is resolver then I am going to create another function and this
function is make which is just going to accept one parameter which is name and
then I am just returning this bindings and get the element with the name
whatever we are passing here and that's it so the simple container class is
having three things three important things binding variable bind function
make function so let's create a new of this class a new container then I will
say var dump or let's use print R and container before that let me bind
something so I will say container bind and here I will say game and because we
said that on this bind function we need to use the column that means a function
or we call it closure so let's return football so now we are going to resolve
this so how we can resolve just say container make again this make function
and pass game so let's close this line and now let's go to the chrome test dot
app and we are getting just an empty object so why we are getting empty
object let's see okay so here on this make class we need to actually run that
function so now if I go you can see we get the football and if I change it to
football one or whatever it is so yes we can see your football one is there this
means we can bind something to container and we can resolve or we can get
something from container and actually this bindings variable is all about the
container so what container is so container is just bindings or an array
in which you can put some object whenever you need it you can get that so
now let's go and see the level implementation of this container so here
we have the container from this application class we know that this
application external container and container classes there and now you can
see bindings and method bindings and some of the other variables are here so
this is actual implementation of container in level obviously there has
to be some bind class so if I see here we have the bind class and at the bottom
you can see this binding they are calling abstract we call it neem so then
we are passing compact with concrete and shear so let me show you where these
things are available so what I am going to do in this web dot PHP I will die
dump app so let's create server yeah it's already running so now here if i
refresh you can see we are having list of all these things which this
application class have and no surprise we have this bindings resolved method
bindings which we get here so at the top we can see we have resolved bindings
method bindings currently this level bind 41 items inside this binding or we
can say inside the service container so that's cool but how we can add some
items to container or how we can get items from the container it's very easy
again go to web dot PHP and if you want to add item inside this container just
like we have the syntax here we can use same syntax here so app bind game and
closer then return football super easy so last time it was 41 so if i refresh
now you can see we have a 42 bindings this means at the bottom we can see our
game binding which is having array of concrete and sheer concrete is
the closer we have passed inside that app binding so this is how you can add
something inside the container or we call it a service container of level but
what's the use of these bindings why not we can just say inside an array and we
call a Reiki as game and value as football what's the use of this finding
so let's just see one important thing how we can get something from the
container so we need to say meek and in fact the make I'll say game so I think
you know what's going to happen it will return football so yeah we have this
football that's cool but instead of just a string why not pass a class so create
a new game class so I will create a class called game it's just an empty
class nothing is there so what's going to happen yeah it has it has the class
of game so you can see whenever we want to use a new class every time we do new
of game in all of your project you need to do this every time so by using this
bindings you have just implemented new game one time and then everywhere else
on your application you just use app make game so if you want to change
something here that will affect it everywhere all over your project not
only this suppose this game class have some dependency so I'm going to create a
constructor so construct and this time it has a dependency of football class so
this time I will say football is equal to football football okay so yeah you
can see it's saying you need to pass some kind of football class so this time
inside game new game we need to say new football but we know there is no
football class so we get error that's the same at a class football not found
let's quickly create that class so football is an empty class
similarly okay and now this time if I go refresh we get this class which is which
depends upon this football class so now again you can see how much easy whenever
you want to change this football to another game or some another kind of
dependency you just change at one place and because we are using app make game
it will affect everywhere but now a new problem arise what if this football
class depends upon something else so again a create construct and a stadium
is a new class so this stadium stadium is equal to stadium and we know yeah we
need to pass new stadium and this class is not there so let's create a stadium
class and because we are doing everything correct it will resolve it
and it will give you the exact implementation of this game class game
require football football require stadium but what if if stadium also
require something so now you can see this is becoming more complex and hard
to manage now because we are using service container because we are on
level we don't need to worry about all these things because everything is take
care by level so even if I comment this out you know service container is very
smart and it know that this game depends upon this football so it will find the
football class so when it get the football class
it will say that ok football class depends upon the stadium and in that way
it will inject every dependencies on your class and this happens because of
PHP reflection class so if you search for PHP reflection class what this
reflection class will do it just give you exact information about the class so
how container know that this game requires something or require what thing
so that's because of this PHP reflection class it will get whatever the
dependencies we have then it will find that dependencies and so on so because
everything is taken care by a container even if we are not binding anything we
are just saying resolve or make game we can also use resolve function so we can
say resolve game and by doing this we don't have to bind anything it will bind
everything by its own and now if I go refresh this page you can see the same
implementation is there and that's because of this service container so you
saw that just by doing this container make bind and bindings kind of things we
are having so much of flexibility on our code that we can easily move along and
create a highly scalable project not only this there is one more thing if you
want to create instance every time you want to use some class so like you can
say app instance and you will say game and function I will just going to return
a closer so I will say instance and this time I will show you app and if I do
this this time instead of bindings we have a new instance for 27 instance and
this is just the closer so it is some kind of concrete implementation of
for some class and bindings is just more flexible kind of thing so all these
things resolving bindings instances are inside service container so last thing
I'm going to show you is let's remove everything from here so here I will say
app bind and this time I will just create random and giving a closer here
let's return STR random so we are having a random string and then died dump app
make and inside that and say random so instead of diadem I will just use dump
why because I want to use another implementation or another resolving
thing for random so if I go refresh this page so you can see we have two
different random string so every time i refresh we get two different random
string why because every time we are calling this app make it will execute
this function but if you want to execute it only one time and use it on your
project you just need to say singleton singleton means it's just going to run
one time it will remember the result and then whenever you call whenever you
resolve your function or your binding it will give exact same thing so to resolve
but we will get same string for both of the resolves so that's because of this
singleton and this is just the difference between binding and singleton
so I think we get the idea of what is container container is just a list of
all the bindings in just a single array called bindings inside this container
class now we know that container is a array of items where we can put
something we can get something wherever you want whenever we want and that's how
that will make things so much easier for us so we will meet in the next episode
till then goodbye