Disease Treat

Know About Ulcers Blog


So, now we know what a class is,
and what methods and properties are, as well as
what inheritance is. We, basically, know
[? OOP ?] by now, but we don’t yet know
how to utilize it to its full potential. So, in this lesson we’re
going to be covering dependency injection,
and the easiest way to explain this to you
is by demonstration. So, I’m going to create
two files, chest and lock. And these are both
going to be classes. So class Chest, and class Lock. Now, the lock method will
contain a protected property of is locked, as well
as a method called lock. And [? a get of ?]
is locked, as well. And all this is going to do
is return this, is locked. Now, inside here,
all we’re going to do is, we’re going to say
this is locked equals true. So, we locking the lock. Next, inside chest, we’re going
to have Public Function Close. And this will have a
acumen called Lock. So, when we set this to
true, it will automatically lock the chest. When we said it to false,
it will close the chest, but leave the lock unlocked. So, here we can’t do
anything just yet, because we can’t access
this property here. So, the first thing that
we have to do is fix that. So, the first thing,
that we need to do is, create a constructor. And we’re going
to inject a lock. And we’re going to
set that to this lock. And we’re going to
create that property up here, Protected Lock. Now what we can do is, we
can say this lock lock, but we only want to
do that If we tell this function to lock the chest. So, if lock equals true– in
fact, that should be identical, instead of equals– and
that should all work fine. And what we could do is, we
could also have protected is lock is closed–
sorry, so, is closed– and we could create
a [? getter ?] for that, is closed. Return this is closed. And up here, all we’d do is,
this is closed equals true. Then what we could
do is, we could add another method called open. And we could say, if
this lock is locked, then we want to unlock it. So, the first thing
that we have to do is create that method, here. So, Public Function Unlock. And all we’re going to do is
set this is locked equals false. So, now what we can do, here,
is we can say, this lock unlock. So, we’re just invoking,
or calling, that method. And then all we have to
do is open the chest. So, is closed equals false. So now, let’s test this out. In our index file,
we’re going to include these two files,
so, chest and lock. And we’re going to instantiate
chest, chest– a new chest, sorry– and as our construct
parameter, we’re taking lock. So, we need to say, new lock. But let’s just make
sure that we’re not going to get any errors,
which is all fine, we’re not getting any errors, here. So, what we can do now is,
we can say, chest lock. And just to make
sure this is working, in fact, that should be close. And just to make sure
that this is working, we’re just going to
say here, echo closed. And we’ll do the same down
here, we’ll say, echo opened. So now, if we refresh,
we get a missing– OK. We need to specify where
we want to lock the chest. So, we’ll just say, you know,
we’ll make this optional. So, we’ll set that
to true by default, so we don’t have to
pass it in every time. There we go. The chest is closed. Then we can call chest
open, and the chest is open. Now, one issue that we
have here, is we can, instead of passing in an
object here, what we can do is just pass in a string. And then we’re going to get
an error, because this lock method, here, doesn’t exist
on a string, obviously. So the way that we can fix
that is by doing something called hyping, a type hinting. What all that means
is that we specify the class before the argument. So now, if we do this
again, and we hit refresh, we’re going to get a
catchable fatal error. And basically, this
ensures that anything we pass into this
constructor, here, or at least the first argument, is going
to be an instance of lock. So if we just fix
this, hit refresh, we get the same result. And this is what we call
dependency injection. Now, I’m just going to give
you a really quick example of a real life situation
where this might be useful. So, I’m going to
delete these files. Close them, get
rid of all of this. I’m going to create a new file. And this is going to
be called Database.PHP. And then I’m going to create
a new file called User.PHP. And again, these are going
to be classes, so Class User, and Class Database. And our database is
just going to contain one method called query. And we’ll probably
pass in the SQL. And all we’re going
to do is we’re going to say something like this
PDO at Prepare SQL, Execute. And we’re not
going to be testing this, or anything like that. This is just an example. OK, so, the next
thing we need to do is add a method
to our User Class. So, for this example,
I’m going to say, Public Function, Create. And this will take
an array of data. So, what we need to
do now is we want to create a user by inserting
him into the database. But how do we access
this method, here, because we want to create a
query inside of this class. Well, again, we use
dependency injection. So, we create a constructor. We type in this database, and
we’ll call it DB for short. And we’ll assign it to a
property, Protected DB. And now all we have to do
is, say this DB Query– is that we called it?– yep. And then we can say, insert
into user’s blah, blah, blah, and that will all work fine. All we have to do is
include these files, Database.PHP, User.PHP. And we’ll say user equals
new user, new database. And just for now, we’ll get rid
of this, and we’ll echo SQL. And then we can
say, User Create, and we’ll pass in an
array user name, Terry. Hit refresh, and
we get this query. And, of course, if this
was on a real site, we’d actually be
executing this query, and saving the user
into a database. Now, another reason– I don’t
want to get into this too much, because the video
will be too long– but another reason for
dependency injection, is [? tight ?] coupling. Now, something that you
may have seen before, is public static
function get instance. And let’s just remove all this
dependency injection stuff. And in here would
say something like, we’d create a properly
protected static instance. And we’d say, if not
static instance– and don’t worry if you don’t
understand what this means, we’ll be covering static in
another future video– so, if not static instance–
so if it’s null, or if it’s not set– then we’re
going to instantiate this class and store it in that variable. So, static instance
equals new self. So, all this means
is new database, but it means it’s dynamic. So, if we change this
to something like DB, then we don’t have to
bother changing this. And then all we do is
return static instance. Now inside a user
class, here, what we’d say is, we’d say DB
equals database, Get Instance. And then we could say
DB Query, like so. And we’d get the same result. But the problem with this is,
we’re tightly coupling user to the database. So, let’s say for
example, we want to change a storage system. Let’s say we want to store
our users in a file, instead. Well, every time that we
get the database instance, we’d have to go through this
class and change it to file. You see? So, dependency injection
eliminates this. All we’d have to change
is the constructor. And you can even
take additional steps by using things like the
repository pattern– which you may be getting into later–
to easily change your storage system.

Leave a Reply

Your email address will not be published. Required fields are marked *