PHP Objects – Inheritance (Part 4)
Articles Blog

PHP Objects – Inheritance (Part 4)


So the next topic is Inheritance. And the idea of inheritance is you take
a class and you wanna make a new class. You can think of it as extending it and adding a little bit of
special sauce to a class. And it’s a really a good pattern, much
like functions, of not repeating yourself. If you have some functionality. Unlike a base class or
unlike core class, and then you’re gonna specialize a little bit. It’s just another form of how
not to repeat yourself and how to structure your code. Now, a big problem that people
have when they do Object Oriented programming is they figure that if they’re
not doing inheritance, they’re not cool. And I will say that’s absolutely not true. If you go for five years and
you never do inheritance, it’s important to understand it,
but don’t feel bad. You could write objects, you could
use objects and never do inheritance. You’re using inheritance when you’re
using library code, cuz we use it a lot. The day that you start writing
a sophisticated piece of library code, for other people to use. You’ll be like,
thank heaven for inheritance. But don’t feel like the goal. I mean, one of the biggest mistakes people
make in object orientation is just trying to overuse inheritance. So understand it but don’t feel like you
gotta show off and use it just to use it. [COUGH] So, we have a parent class and
child class. The child class inherits
everything from the parent class except that which is overridden or
added in the child class. Another word for this is subclasses. It’s class and a subclass. I think of it also as
extending in some languages, like Java, they use the word
extend to say extend this thing. Okay, so here we go. So let’s take a look at that
code that we wrote before. And again this is a silly, silly example
so that I can fit them on a screen. But remember the class hello from before,
it’s got a language. We construct it and get the language and
we have a greet, right? And so now what we’re gonna
do is make a new class, so that class is there
the way it was all along. So just assume that code’s there. And now we say is class Social,
new class extends Hello. So what that basically does is
that pulls in all of Hello. The language, the attribute language,
the constructor, and the greet method is all pulled in. You don’t have to repeat that at all. And we’re gonna add one function,
a function called bye. And so this is the logic for
the function, bye. And so now we can create a new Social,
passing in es which is really calling this constructor that’s
been inherited from the Hello. And then we’ve got a hi variable, hi. And it’s got a greet method, but this greet method is really
been inherited from the Hello. And then we have a buy method, that is the
part that we’ve added in the Social class. So that’s how that works. Is that it really is
pull all this stuff in, fill up that class with all
the stuff in the Hello class. You could still make a Hello object or
a Social object. Hello didn’t stop existing
because we added it. We just copied it and then extended it. So inheritance is the ability to take
a class and extend it to make a brand new class, without harming
the first class on the first place. Now I’ll talk a little bit,
now that we’ve talked about classes and inheritance, about the more
sophisticated bit of visibility. I’ve told you about two
levels of protection. One is public which means that the
variable can be accessed inside the class and outside the class. Private means it can only be
accessed inside the class, that’s the new one we
haven’t shown before. Protected means it can be accessed
in the class, not outside the class, but in any derived class,
in any child class, in any subclass. So that’s protected, and so
I used protected earlier, but private is the most protective thing. Protected means I’m sharing this data with
subclasses and not the outside world. So let’s just take a look
at some sample code. With protected and private. So here’s class. We got a public variable,
we got a protected variable and private variable, named pub,
pro and priv respectively. And so in this code, inside of a method
that’s inside a class, you can look at and touch the protected. You can touch the public,
the protected and the private. In the main code, this is the outside,
so again, this is the inside the class. This is outside. And part of this is so the class can sort of build a protective
barrier around itself and say, look, I’m gonna let this little bit
here sneak out so you can see it. But I am not going to let you see
these things, you cannot see them. I’m hiding them from you, okay? And that’s mostly because it doesn’t
want this outside code, that might not understand what it means to change that
variable, to be changing the variable. So just please don’t change the variable. Well, you’re not allowed
to change the variable. So a public variable. A public variable can be read,
it can be modified outside of the code. But a protected and a private will
blow up, it’ll just fail right, right? And printHello is a public function. You can actually make private functions
and protected functions as well, methods. Okay, so now assume all that. We have got MyClass with public,
protected and private, and now we’re gonna make a subclass,
an extension. So we’re gonna extend it
by extending MyClass, and putting all of the attributes and
methods in to MyClass. And we [COUGH] now have printHello. This printHello is not
the same as printHello here. Oops, we’ve actually overridden it. And so now we’ve imported everything
from this class except printHello, we’re gonna define
differently in the subclass. And so within printHello we can look
at the public stuff, we can look at the protected stuff, but we cannot see the
stuff that is private in MyClass, right? We can look into the protected
stuff in MyClass and modify it, and touch it, and look at it. But we can’t look at the private stuff. And so private means,
I am putting such a wall around this data that even if you make a derived class,
you still can’t see this variable. And don’t worry so much about why,
just understand that there is this notion of wrapping data, and having different
levels of protection for that data. When we make a new class here we
can still access the public data. That public data was originally
from MyClass not MyClass2. But that works, but
of course the private and protected can not be seen
here in the outside world. Okay, so that’s how that works. Don’t worry to much about it. I really am more interested in
your understanding the concepts. And the last bit I wanna talk about
is something that I don’t recommend. These days, we mostly make classes
exactly the way I just showed you, but a lot of older code
would actually construct objects from scratch
without it having a class. So this is kind of a class. This is an object without a class, okay? And some people prefer this over just
putting a bunch of string keys in an array cuz it’s a little cleaner, it’s more it
seems a little tighter than just strings. And so here we go, and there is a string
called stdClass, standard class. And so what it does, a standard
class is like a class that is empty. It has no constructors, it has no
attributes, it has no methods, okay? So you make it, and you have a class. And so what happens now, is there
is a object called player out here. Right, there’s a player object, and
we start putting stuff into it. We start changing attributes, right? So we can make a name attribute and
stick chuck in there, and we can put a score attribute and
make it be zero. We could add one to the score attribute. And so what’s happening is
there was nothing defined here, it’s like an empty class. And we’re defining it
as we write our code. We just creating variables inside of it. And so that’s one of the things,
is you can take and put variables inside attributes, inside classes,
if you really want, right? And so there we go. So that’s a class, if we’re gonna
define the class we could do something like this class player, there’s
a name variable and square variable, but this is sort of
inventing it from scratch. And so
here in a none sort of from scratch, you just make the class,
you construct the class. The class has two variables in it, and the you can do things like access
the attributes and print them out. So you can kind of achieve the exact
same thing by defining a class. This is kind of old school, and so the older the code is, the kind of less
it feels comfortable about classes, and it sort of treats classes as
a variation on associative arrays. The more modern stuff, and
increasingly you’re gonna see people really defining classes and putting
the attributes and the functions and the methods right in those classes,
and doing all that stuff. So, I would totally understand if
you watched all these lectures. You’d be like Object Oriented
programming just scrambled my brain. Because that is a completely
rational answer and reaction to the first time that you
see Object Oriented programming. And so all I’m doing here is desensitizing
you to Object Oriented programming. I’m not expecting you to be a whiz,
I’m not gonna ask you to write objects. All I want you really to be able to do is
read Object Oriented documentation and construct a PHP that makes use of objects. So we’re gonna be using PDO and
other kinds of things in this class. And I wanna be able to
say it’s the PDO class. And then there’s the connect method and
the XYZ method. And the attributes that’s this,
and it’s a static attribute, so that’s the colon colon operator. I want you to be able to look at code and I want you to be able to
look at documentation. And that’s really what my goal was,
but these concepts of templates and instances and
inheritance are powerful and profound. And if you go far enough in programming,
you will come back to this and you’re gonna go like, I’m so
glad that they built that stuff. But that’s not here at the beginning. So I hope this was helpful,
I hope you took out of this what you need. Don’t feel like you need to master this, just kinda understand the terminology,
okay? See you on the net. [MUSIC]

Leave a Reply

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

Back To Top