What is OOP? Real World Objects in Code
45sClear analogy makes complex concept accessible to beginners.
▶ Play ClipThis tutorial provides a comprehensive overview of object-oriented programming (OOP) in PHP. It covers fundamental concepts like classes, objects, inheritance, polymorphism, and encapsulation, along with practical examples. The video also demonstrates advanced features such as interfaces, abstract classes, and magic methods.
OOP models real-world objects using classes, which define attributes and methods.
Inheritance allows a class to inherit attributes and methods from another class.
Polymorphism allows different classes to be treated as instances of a common superclass.
Interfaces are contracts that force implementing classes to define certain methods.
Access modifiers: private (only within class), public (any code), protected (class and subclasses).
Static attributes are shared across all instances of a class.
Encapsulation protects data by restricting direct access and using getters/setters.
Constructors initialize objects; destructors handle cleanup.
Magic methods like __get and __set provide shortcuts for accessing protected attributes.
Method overriding allows a subclass to redefine a parent method.
The 'final' keyword prevents a method from being overridden.
The __toString magic method defines how an object is represented as a string.
Interfaces enable polymorphism; a class can implement multiple interfaces.
Static methods can be called without instantiating the class.
Abstract classes cannot be instantiated and force subclasses to implement abstract methods.
"The title is accurate; the video thoroughly covers OOP in PHP from basics to advanced concepts."
What is a class in OOP?
A blueprint for creating objects, defining their attributes and methods.
1:06
What is an object in OOP?
An instance of a class.
1:06
What are the three access modifiers in PHP?
Private, protected, and public.
4:08
What does the 'private' access modifier mean?
Only methods within the class can access and modify the data.
4:14
What does the 'public' access modifier mean?
Any code can directly access and modify the data.
4:19
What does the 'protected' access modifier mean?
Like private, but subclasses can also access and modify the data.
4:26
What is a constructor in PHP?
A special method called when an object is created, used for initialization.
7:59
What is a destructor in PHP?
A special method called when an object is destroyed, used for cleanup.
9:12
What is inheritance in OOP?
A class inherits attributes and methods from another class.
1:27
What is method overriding?
A subclass redefines a method inherited from its parent class.
1:51
What is an interface in PHP?
A contract that forces implementing classes to define certain methods.
3:11
What is an abstract class?
A class that cannot be instantiated and forces subclasses to implement its abstract methods.
23:56
What is a static method?
A method that belongs to the class itself, not to any instance, and can be called without creating an object.
21:31
What is polymorphism?
The ability of objects of different classes to be treated as objects of a common superclass.
2:13
What is encapsulation?
The practice of keeping an object's internal state private and providing controlled access via methods.
6:48
Classes as Blueprints
Fundamental OOP concept: classes define the structure and behavior of objects.
1:06Access Modifiers
Explains how to control data visibility and encapsulation in PHP.
4:08Constructors and Destructors
Shows how to initialize and clean up objects automatically.
7:59Interfaces for Polymorphism
Demonstrates how interfaces enable flexible, polymorphic code.
18:32Static Methods
Highlights utility methods that belong to the class, not instances.
21:31[00:00] well hello Internet and welcome to my
[00:01] tutorial on objectoriented PHP in this
[00:04] tutorial I'm basically going to cover
[00:05] everything you ever want to know about
[00:06] objectoriented side of PHP and I have a
[00:09] lot to do so let's get into it okay so
[00:13] what I'm going to be using in this
[00:14] tutorial is kodo uh you can use a basic
[00:17] text editor but I just decided to use
[00:18] this because it's free and just to give
[00:20] you another idea what you can develop
[00:22] PHP with and you can find it at kodo
[00:25] id.com download slum sign edit and I
[00:29] also have it in the description now
[00:30] before we start I thought I would very
[00:32] quickly cover exactly or at least
[00:35] roughly what object-oriented programming
[00:37] means now if we have a basic class man
[00:40] what that's going to mean is all men
[00:42] have attributes like the man you'd see
[00:44] here he might have black hair is H might
[00:46] be Bob he might have an array of ties
[00:48] shirts pants and shoes and we also all
[00:52] have abilities or functions or
[00:55] operations that we can perform like
[00:57] every man can talk walk run throw eat
[00:59] and and work now objectoriented
[01:01] programming allows us to model Real
[01:04] World objects inside of things called
[01:06] classes they are the blueprints for
[01:08] creating objects and inside of those
[01:10] classes we're going to define the
[01:12] attributes and the functions or methods
[01:16] that those objects can do and that is
[01:17] the basics of what it means to be a
[01:20] class and what a class does and what an
[01:21] object is we model Real World objects in
[01:24] object-oriented programming that's why
[01:25] it's so awesome so what is inheritance
[01:27] well let's say we have an object that is
[01:30] called man and we want to create a more
[01:32] specific version of a man called Mark
[01:35] well what we're going to do is Mark is
[01:37] going to inherit all the attributes and
[01:40] functions that are in the man class and
[01:43] then we can build or change those
[01:44] attributes and functions in the mark
[01:47] class and that is basically what
[01:49] inherence is and overriding just means
[01:51] that we would go in and say override a
[01:54] function so that a function doesn't do
[01:56] what it previously had done now when I
[01:58] refer to function they are also referred
[02:01] to as methods and operations but they
[02:03] are basically functions some people
[02:05] might disagree with me saying that but
[02:07] either way just to keep this basic you
[02:08] can think of methods functions and
[02:10] operations as being all the same things
[02:13] so what is polymorphism aside from being
[02:14] a big word well like we just saw there
[02:17] mark inherited all the attributes and
[02:20] all of the functions or methods from the
[02:22] man class whenever he created the mark
[02:25] class now if he goes into the walk
[02:28] method and instead of just printing out
[02:30] walk he instead says he wants to print
[02:33] out walk fast whenever Java sees that it
[02:36] is getting a man object that is trying
[02:39] to walk it is going to print out walk to
[02:42] the screen and whenever it sees the mark
[02:45] class it's going to say oh you inherited
[02:48] the walk method from the man class we're
[02:51] going to go in and make certain that you
[02:53] didn't change the walk method in any way
[02:56] jav is going to see that we instead want
[02:58] to walk fast with the mark object and
[03:00] hence it's automatically going to print
[03:02] that out now polymorphism can get a lot
[03:04] more complicated from there but that is
[03:06] just a basic gist or something just to
[03:08] understand and you'll understand a lot
[03:09] of how polymorphism works and that
[03:11] brings us to interfaces what is an
[03:13] interface well basically when our class
[03:17] Mark decides that it wants to implement
[03:20] an interface what it's in essence doing
[03:22] is signing a contract that says I the
[03:24] mark class promise to implement any
[03:27] methods or functions that are in the
[03:29] interface and I'm going to show you
[03:31] examples of all these different things
[03:33] right now okay so this time I have
[03:35] Komodo on the left and I have Google
[03:37] Chrome on the right and I have my server
[03:40] set up in Local Host and I'm just going
[03:41] to execute things first thing I'm going
[03:43] to do is command and create a class well
[03:45] all you do is type in class and in this
[03:47] situation I'm going to type animal and
[03:50] that is going to be it I'm going to make
[03:51] some changes to it later of course and
[03:53] then inside of this what I'm going to do
[03:55] is I'm going to define the attributes
[03:56] that I want my animal to have well the
[03:58] very first I'm going to do is I'm going
[04:00] to type protected in here and there are
[04:03] different ways to categorize both
[04:05] methods as well as attributes you can
[04:08] have protected you can have public and
[04:11] you can have private now private means
[04:14] that only methods or functions in the
[04:16] class can access and change this data
[04:19] public means that any code can directly
[04:22] access and change the values for these
[04:24] attributes and protected is just like
[04:27] private meaning that only method in the
[04:30] class can access and change this data
[04:32] however when we create a subass like we
[04:34] did Mark from the man the subass is
[04:38] going to still be able to change the
[04:40] value for this attribute so I want the
[04:44] protection of private but I also want
[04:45] sub classes to be able to inherit all
[04:48] this data so if it was Private couldn't
[04:50] do that just stick around here you'll
[04:51] get it so I'm going to create a couple
[04:53] protected attributes and let's say that
[04:56] all our animals are going to have names
[04:58] and all I'm doing here is to defining
[05:00] the attributes we have and let's also
[05:02] say that each of these animals is going
[05:04] to have a favorite food not favorite
[05:07] name favorite food protected again
[05:09] they're all going to make a specific
[05:11] sound and they are all going to have a
[05:14] specific ID number this brings us on to
[05:17] the next thing I want to talk about
[05:18] which are static attributes and this
[05:20] time I'm going to make this public so
[05:22] anybody can access this and let's say
[05:25] that every time I create a new animal
[05:27] object I want to automatically increment
[05:30] the number of animals it's going to
[05:32] start off at zero because we haven't
[05:33] made any animals yet and what static
[05:36] ultimately means is that every object of
[05:39] the type animal that is ever created
[05:41] they are all going to share number of
[05:44] animals so if this number goes up to one
[05:46] it's going to go up to one for every
[05:49] other animal object that is created and
[05:51] if you ever want to access a static
[05:55] value and make a change to it I'm going
[05:56] to show you how to do this later you're
[05:58] just going to type in the class name
[05:59] with two colons and then number of
[06:02] animals and then you could go and add
[06:04] one to this or do whatever I'm going to
[06:06] actually show you this in an example in
[06:08] a second another thing we can have
[06:09] inside of here are constants and there's
[06:12] a constant and let's just say I want to
[06:13] put Pi in here for some Bizarro reason
[06:15] so I'm just going to put in
[06:18] 3.14159 and there we go and a constant
[06:21] just means that it's going to be
[06:22] constant and it's never going to change
[06:23] its value if you want to refer to that
[06:25] once again you're going to type in the
[06:27] animal class name or the class last name
[06:30] two colons and then Pi in this situation
[06:32] and that's going to get you access to
[06:34] that and once again I'm going to show
[06:35] you that in a second now you're going to
[06:37] Define functions inside of classes just
[06:40] like you define functions in anything so
[06:43] let's say I want to create a function
[06:44] that is going to return the name this is
[06:47] a situation in which we have something
[06:48] called encapsulation not that
[06:50] complicated remember when we talked
[06:52] about private and protected and public
[06:54] well by marking these as protected or
[06:57] private we are encapsulating or protect
[06:59] ing that data we are saying that we
[07:01] don't want anybody to come in here and
[07:03] willy-nilly be able to change the data
[07:05] what we're going to say instead is if
[07:07] they want to get the name for my animal
[07:10] they're going to have to call the get
[07:12] name function and when they do we're
[07:14] going to return it and we encapsulate or
[07:17] protect data so that they can not go in
[07:20] there and do things like change the name
[07:22] of my animal to a number or something
[07:25] nonsensical okay just to protect the
[07:27] data and as you can see right here if if
[07:29] we want to refer to an attribute inside
[07:32] of a class what we do is we type in this
[07:35] meaning this specific object and we want
[07:39] the name that is stored for that
[07:42] specific object this is basically used
[07:44] as a generic way to say that we want the
[07:47] name for whatever the current object we
[07:49] are using because it's not going to be
[07:50] able to address the object by name so we
[07:53] just use this now another functions that
[07:56] is very useful is called a Constructor
[07:59] and to create that we just type in
[08:02] construct with two underscores in front
[08:04] of it and then what this guy is going to
[08:06] do is initialize things or set some
[08:09] values whenever we create every animal
[08:11] well what do you think would be a good
[08:13] value to change well we're going to need
[08:15] an ID for our animal that's going to be
[08:17] random and unique and we're going to
[08:19] have to increment the number of animals
[08:20] when we create a new animal object so if
[08:23] we want to create ourselves a new ID we
[08:25] want to refer to our ID for our animal
[08:28] object that we created we just go this
[08:31] again with that little arrow and then we
[08:33] go Rand and then if we want to generate
[08:35] a random number let's say between 100
[08:37] and 1 million we would just do that and
[08:40] there we go so now each animal that we
[08:42] create is going to sort of have a random
[08:44] number and let's say that we want to
[08:46] Echo out to the screen the ID this like
[08:49] that ID and then we'll put some text in
[08:51] here has been assigned and then we'll
[08:53] throw a break statement in there at the
[08:55] end with a semicolon of course and now
[08:56] I'm going to show you how to access the
[08:58] static attribute that I talked about
[09:00] before we're just going to type in
[09:01] animal two colons and then we're going
[09:03] to type in number of animals and we are
[09:05] going to increment it because we created
[09:07] a new animal object another thing we
[09:09] have available to us is what's called a
[09:12] Destructor and let's just put public in
[09:14] front of that and two underscores and
[09:17] destruct now the destructor is going to
[09:19] be called when all references to an
[09:21] object have been unset so if there's
[09:23] some housekeeping you want to do
[09:25] whenever an object is deleted you could
[09:27] do so in this situation what I'm going
[09:29] to do is I'm just going to print out to
[09:30] the screen that the object has been
[09:32] deleted is being destroyed of course you
[09:35] would never use this but in your uh
[09:37] website but whatever put a little frowny
[09:40] face there and there we go now remember
[09:41] up here where we were creating the git
[09:44] name function so that we could get the
[09:46] value of name for our different
[09:48] functions well PHP actually provides a
[09:51] shortcut way to be able to create what
[09:53] are called Getters and Setters Getters
[09:55] are going to get you the value of these
[09:56] attributes and Setters are going to set
[09:58] the values and because they're protected
[10:00] that means we need to use a function to
[10:02] change these values and get these
[10:04] attributes and these little handy dandy
[10:06] things are called Magic Setters and
[10:08] Getters and these are actually magic
[10:10] methods as well anything with an
[10:12] underscore is a magic method so if we
[10:14] want to define a function inside of this
[10:17] guy that is going to be able to get
[10:19] anything we're going to type in function
[10:21] and then we're going to type in get and
[10:23] we're going to pass the name of the
[10:26] attribute that we want to get to this
[10:28] function whenever ever it's called and
[10:30] if we want to print something out to the
[10:31] screen just to show that something's
[10:32] going on here we could do something like
[10:34] asked for name which is going to be the
[10:37] attribute they're asking for and a break
[10:39] statement again and then if we want to
[10:41] return it the attribute we can just come
[10:43] in here and go this and the name that
[10:46] was passed inside of here so we're going
[10:47] to have to put a dollar sign and name
[10:49] because it would get confused otherwise
[10:51] it would think this was the name we're
[10:52] looking for this is going to return
[10:54] whatever attribute is passed in here and
[10:55] I'm going to show you examples of all
[10:57] these here in a second now if we want to
[10:58] set the value this time I'm going to do
[11:00] something a little bit more complicated
[11:01] just to do something a little bit more
[11:03] complicated I'm going to check that the
[11:05] attribute that they sent in here was
[11:07] actually a valid attribute now with this
[11:09] you're going to pass in the attribute
[11:10] name and the new value that you want to
[11:13] set for set attribute and let's say I
[11:15] want to check that the name they passed
[11:17] in is actually a valid attribute one
[11:19] thing I could do is use a switch
[11:20] statement here and then I can come in
[11:21] here and go case and I'm just like I
[11:23] said I'm just doing this to show you how
[11:25] to do it and then we could come in here
[11:27] and say if it's name well we know name
[11:28] exists this so set name to whatever the
[11:32] value is and then we can do pretty much
[11:34] the same thing well we're going to throw
[11:35] in a break statement also there we go
[11:37] and we could do the same thing with
[11:39] favorite food do the same thing with
[11:41] sound once to set the sound for my
[11:44] animal and then finally if it's none of
[11:46] those attributes what I'm going to say
[11:48] is I'm just going to Echo out to the
[11:49] screen name not found and then after all
[11:52] that's done I get out of my switch
[11:54] statement I could come down here and
[11:56] just to print something out on the
[11:57] screen I could print out set name name
[11:59] to Value so there that is and that is
[12:02] how the little magic methods for setting
[12:04] and getting are used and like I said
[12:06] before inheritance occurs whenever you
[12:09] create a new class by extending another
[12:11] one and like I said we're going to
[12:13] inherit all the attributes and methods
[12:15] that are defined in the super class
[12:17] animal in this situation is a super
[12:19] class inside of the subass so what we're
[12:22] going to do is we're going to create a
[12:24] new class and it's going to be called
[12:26] dog and if we want to inherit everything
[12:28] from animal we're going to type in
[12:30] extends and then animal and later on
[12:32] I'll cover interfaces now let's jump up
[12:34] here and let's create a function that we
[12:37] are just going to override so we'll
[12:39] create a function called run inside of
[12:42] our animal class we're in the animal
[12:43] class right now and let's say this one
[12:45] goes Echo this name runs we'll throw a
[12:49] braak statement in there and then we'll
[12:50] actually just copy this cuz we're going
[12:52] to override it and as you can tell I
[12:54] don't need to put the attributes inside
[12:55] a dog that are an animal they're
[12:57] automatically there so name is inside of
[12:59] this and let's say that our dogs run
[13:01] like crazy and that's how you override a
[13:04] method so now what I'm going to do is
[13:05] I'm going to jump outside of the dog and
[13:07] animal class and start creating them and
[13:10] you can watch everything sort of execute
[13:12] here over inside of chrome so I'm
[13:14] outside of both of the classes and if I
[13:16] want to define a new animal I just go
[13:18] animal and let's say I want to say
[13:20] animal one is equal to new animal and
[13:23] there we go and the Constructor this guy
[13:25] right here is going to be called
[13:27] whenever I create this new new animal
[13:30] and I could also pass in attributes for
[13:32] the Constructor but I decided not to do
[13:34] that now what I'm going to do is come in
[13:36] and set the attributes for my new animal
[13:38] object I created and I'm just going to
[13:40] type in name and I'm going to give it
[13:42] the name of spot and then I'm also going
[13:45] to set favorite food equal to meat and
[13:48] then I'm going to set the sound that my
[13:51] animal makes to rough now if I want to
[13:53] print out all this new data onto our
[13:55] screen I could do something like Echo
[13:57] animal one name
[13:59] says and then call the sound for my
[14:02] animal and then go down to the next line
[14:05] and we'll say something like give me
[14:07] some and then let's say favorite food
[14:09] and then let's say we also want to print
[14:11] out the ID number here my ID is put
[14:14] another period and all the code is of
[14:16] course available in a link in the
[14:18] description and then if I want to print
[14:19] out the ID I just go ID and then if I
[14:21] also want to print out the total animals
[14:24] that have been created go total animals
[14:26] is equal to and then if I want to call
[14:28] that stack IC value number of animals
[14:31] remember I just type in the class Name
[14:32] colon colon number of animals put
[14:34] another period inside of there throw two
[14:36] break statements in there and there we
[14:38] go now I can go over inside of chrome
[14:40] and execute this and you're going to see
[14:42] that the ID number has been assigned the
[14:44] name of spot has been assigned Favorite
[14:47] Foods meat sound is rough I called the
[14:49] getter to get the name the sound and the
[14:52] favorite food and then it printed out
[14:53] all of that information on the screen
[14:55] and then you can also see here that spot
[14:57] was destroyed after the object was no
[14:58] longer longer needed this is the
[15:00] destructor function right there if I
[15:02] wanted to come in here and also print
[15:03] out the favorite number just to show you
[15:05] how to print a constant from a class
[15:07] favorite number again just type in the
[15:10] class name and piie and we'll throw in a
[15:12] break statement and there you can see
[15:14] favorite number printed out as well now
[15:15] I'm going to do exactly the same thing
[15:17] except I'm going to use the dog class so
[15:20] I'm going to come in here and just copy
[15:21] all this because there's not much I need
[15:22] to change paste that in there and I'm
[15:24] going change this to animal 2 and it's
[15:27] going to be of type dog change that that
[15:29] to two change that to two change that to
[15:31] two two two two two and I can change
[15:35] this to dog and it's still going to be
[15:37] able to get it and then I'm just going
[15:38] to come in here and change the
[15:39] attributes I'm going to change this to
[15:41] Grover I'm going to change this to
[15:43] mushrooms just to be weird and I'm going
[15:45] to change this to G save it over here
[15:49] execute and there you can see we created
[15:51] a new ID number the name is set to
[15:53] Grover all those things are working
[15:55] Grover says G give me some mushrooms my
[15:57] ID is blah blah blah blah blah one thing
[15:59] you might find weird here is animals
[16:02] shows up as two there are still two
[16:04] animals created and even though we are
[16:06] referring to a dog in this situation it
[16:09] is still going to increment the numbers
[16:11] because it ultimately calls the
[16:12] Constructor inside of the animal class
[16:15] now the next thing to talk about here is
[16:17] we went in here and we overrode the Run
[16:20] class so here we have runs for our
[16:22] animal class and here we have runs like
[16:25] crazy for a dogs class so I'm going to
[16:27] show you exactly how that works Works
[16:29] come in here and just go animal one and
[16:31] if we want to run a method inside of
[16:33] here we just type in whatever the
[16:35] object's name is and then whatever we
[16:37] want to run and animal two in this
[16:39] situation and run and when we execute
[16:42] you're going to see spot runs and Grover
[16:44] runs like crazy and you can also see
[16:46] here Grover is destroyed spot is
[16:48] destroyed now what would happen if we
[16:49] want to actually come into our animal
[16:51] class and Define a function that we
[16:53] don't want to allow the subclasses to
[16:56] override well we're in the animal class
[16:58] right now it's actually quite easy we
[17:00] just type in final and then if they try
[17:02] to override it in the dog class well
[17:04] it's just going to throw up a whole
[17:05] bunch of errors it's not going to allow
[17:07] it to happen so let's say we create a
[17:08] function called what is good and all it
[17:11] says is running is good and once again
[17:13] if we wanted to execute this we would
[17:15] have to come in here and just say what
[17:17] is good again we're going to use the
[17:19] same whatever the object's name is well
[17:21] we could just do this what is good and
[17:24] you can see running as good as right
[17:25] there so even though it's just an animal
[17:27] of course it's going to run inside of
[17:29] the dog class CU it inherited that
[17:32] another thing we could do is you see
[17:33] here where we're printing out all this
[17:35] information well let's say we wanted
[17:37] this information to automatically be
[17:38] printed out anytime the user said Echo
[17:41] and Then followed by the name of our
[17:43] object well we could do that what we're
[17:45] going to do is we're just going to come
[17:46] in here and copy this and then we're
[17:48] going to go up into our animal class we
[17:50] going go right here and we're going to
[17:51] use another magic method and this one's
[17:53] going to be called two string and all
[17:55] two string does is it defines inside of
[17:58] here what happened happens whenever the
[17:59] user says I want to print some
[18:02] information out on the screen whenever
[18:04] somebody goes Echo and follows that up
[18:06] with the name of the object all you're
[18:07] going to do is type in return and then
[18:09] you're going to print out what you want
[18:10] to show up except you're going to want
[18:12] to change all of these specific object
[18:14] names to this so just change these to
[18:16] this this this and then here we're going
[18:19] to change this to animal of course and
[18:21] everything else can stay the same and
[18:22] then you could do something like go Echo
[18:25] animal one just type in the object's
[18:27] name save it execute there you can see
[18:29] it printed out on the screen right there
[18:31] now remember when I talked about
[18:32] interfaces and interfaces are just a
[18:34] contract that says that any class that
[18:36] implements an interface must Define the
[18:38] functions that are inside of the
[18:40] interface and this is a way for us to
[18:42] take advantage of polymorphism so we're
[18:44] going to do here is Define an interface
[18:46] by going in typing interface and let's
[18:49] say we want to say singable we want to
[18:51] give our animals the ability to sing and
[18:54] we're going to come in here and Define
[18:56] the function but we don't say what the
[18:58] function does we just leave it like that
[19:01] and what that means is when we go into
[19:03] say our dog class and say implements and
[19:06] then singable that it is going to force
[19:08] us to Define sing inside of it so we're
[19:11] going to say function sing and if you
[19:14] don't Define anything a function is
[19:16] automatically public and then we're
[19:17] going to say Echo this name sings and
[19:20] then we'll do something like g g g g
[19:24] semicolon at the end and then we're
[19:26] going to do exactly the same thing with
[19:28] our animal CL class so come up here and
[19:30] the great thing about using interfaces
[19:33] is you can only ever extend one object
[19:37] but you can Implement a NeverEnding
[19:39] array of interfaces so that's why they
[19:40] are very useful so singable it's going
[19:43] to say we have to create the sing class
[19:45] inside here or the sing method there we
[19:47] go and there we are and then just Bow
[19:50] Wow Wow and then if we come down here
[19:52] and go animal one or animal 2 or
[19:55] whatever and call sing it is going to
[19:56] print that out on our screen so there's
[19:58] an example using an interface another
[20:00] example of polymorphism is here where
[20:03] we're going to be able to Define
[20:04] functions that are going to be able to
[20:06] accept classes that extend a specific
[20:09] class or a specific interface here's
[20:11] polymorphism in action again so this is
[20:14] outside of any of the classes we're
[20:15] going to come in here and we're going to
[20:16] type in function and we could say
[20:18] something like make them sing and then
[20:21] we could say singable because both the
[20:23] animal and the dog objects are both of
[20:26] type singable and we could just type in
[20:28] singing animal here and then we could
[20:30] just type in singing animal and call
[20:32] sing and it's automatically going to
[20:34] execute the right sing in the right
[20:36] animal even though it doesn't say here
[20:38] that it's of animal type or of dog type
[20:41] it says it's of singable type so we'll
[20:42] be able to just come in here and go make
[20:44] them sing and pass in animal one or
[20:47] animal 2 and when we execute it's going
[20:50] to automatically have the right animal
[20:52] sing see there you go and of course
[20:54] since you can do this with the different
[20:56] classes that implement the different
[20:58] interface of course you're also going to
[20:59] be able to do it with the dog class
[21:01] because it extends the animal class so
[21:04] here what we're going to do is go
[21:05] function sing animal and you can see
[21:07] there it's animal type singing animal
[21:09] once again this is outside of any of the
[21:11] classes and I'm just going to copy this
[21:13] up here so I don't have to type it out
[21:14] again and once again I'm going to be
[21:15] able to go sing animal sing animal and
[21:18] then let's just copy these there you go
[21:20] execute and you can see it prints out
[21:22] the right things even though all I said
[21:24] here was that I was passing animal
[21:26] objects okay polymorph is in action
[21:29] another type of function you're going to
[21:31] have inside of PHP is what are called
[21:33] Static functions now static functions or
[21:35] methods can be called without the need
[21:38] to instantiate or create an object
[21:41] sounds like a big word but they're not
[21:42] really that's what it means when
[21:44] somebody says instantiate that means
[21:45] they just created an object let's just
[21:47] go into the animal class again and I'm
[21:50] going to type in static meaning this
[21:52] belongs to the class add these and let's
[21:55] say that I wanted to accept a number one
[21:58] and a number two as like a utility
[22:00] function that doesn't really make sense
[22:02] that my animals will be able to add but
[22:04] I wanted to have this in here for some
[22:05] Bizarro reason this is terrible form by
[22:07] the way but I'm just doing this just to
[22:09] show you what a static function is
[22:11] number one plus number two throw a break
[22:14] statement in there and now what I can do
[22:15] is come down inside of here and do
[22:17] something like Echo 3 + 5 is equal to
[22:22] and if I want to call that static
[22:23] function I just go animal which is where
[22:25] it resides and then I call add these and
[22:27] then let's say I pass in three and five
[22:30] and then if I execute it you're going to
[22:32] see 3 + 5 is equal 8 that's a static
[22:34] function and now I'll run through a
[22:36] bunch of other different things you can
[22:37] do with PHP you can check the class type
[22:40] so let's say something like is it an
[22:43] animal we want to check if it's an
[22:45] animal you would go and put something
[22:47] like animal to and then instance of and
[22:50] then it'll say animal and yes indeed
[22:52] animal 2 even though it's a dog object
[22:54] is an instance of the animal object type
[22:57] and then we'll say that we want to
[22:58] assign true if it is or false if it
[23:01] isn't and you could go in here and put
[23:03] other different things inside of here
[23:05] and check the class type on your own
[23:07] Echo do something like it is that animal
[23:12] I'm using single quotes here so I can
[23:13] actually type in animal 2 in this
[23:15] situation with the dollar sign is an
[23:18] animal break statement it is true that
[23:21] animal 2 is an animal see print it right
[23:23] there another thing we could do is clone
[23:25] which is make a clone of an object and
[23:28] you could actually inside of your class
[23:31] Define the things that you want to be
[23:33] copied over to any clones and you would
[23:35] just create a function inside of it with
[23:37] the magic two underscores with clone
[23:39] after it and then that would be called
[23:41] anytime something was cloned but just to
[23:43] keep this nice and short I'm just going
[23:45] to show you how to create a clone so
[23:47] let's go animal clone is equal to and
[23:49] then you would just type clone and then
[23:51] you'd go animal one now we just create a
[23:54] copy of it another thing you can do is
[23:56] Define what are called abstract classes
[23:58] and and also abstract methods now an
[24:00] abstract class cannot be instantiated it
[24:03] cannot have an object be created of type
[24:06] whatever the abstract class is but
[24:08] instead it forces classes that implement
[24:11] it to override every abstract method
[24:14] that exists inside of it you want to
[24:16] create an abstract class you just type
[24:18] in abstract class and then whatever you
[24:20] want it to be called and then inside of
[24:22] it you're going to Define all of the
[24:25] different methods or functions that must
[24:28] be used by any class that implements
[24:32] this abstract class so it's just random
[24:34] function and it could have an attribute
[24:37] in it or it could have whatever so
[24:38] there's an abstract class and then you
[24:40] could also use something that I almost
[24:43] never use which is call inside of a
[24:45] class and if it is defined inside of a
[24:47] class what this is going to do is
[24:49] provide method overloading but since PHP
[24:51] isn't strongly typed there's really no
[24:52] reason to use it now I just wanted to
[24:54] briefly give you an idea that I mean I
[24:56] covered pretty much everything but want
[24:58] to give you an idea of how this is used
[25:00] and how this is used in things like well
[25:02] I'm going to show you this this is very
[25:03] simple let's say I wanted to create a
[25:06] way to very quickly be able to or very
[25:09] easily be able to define the title
[25:11] description keywords an author for my
[25:13] website and also provide ways to get
[25:15] those titles and also provide ways to
[25:18] create the opening part of my HTML
[25:21] document and like I said I'm dumbing
[25:22] this down a lot okay so I would go in
[25:25] and Define all these functions and all
[25:27] this information I then then inside of
[25:29] my website could just do this I would
[25:31] say require and site info.in is the name
[25:35] of this file right here I could just
[25:37] come in here and very quickly whip out a
[25:39] site okay now things normally aren't
[25:41] done that way because you're normally
[25:42] pulling data from databases and setting
[25:44] things up right so what I decided to do
[25:46] is go into WordPress and I did a ton of
[25:49] tutorials and this is how WordPress and
[25:51] every customer management system works
[25:53] this is it so you can see here I'm
[25:55] including data that I want to use or
[25:57] functions I want to execute and you can
[25:59] see right here I have things like gith
[26:01] header this is WordPress and this is
[26:03] automatically going to pull that
[26:04] information from this file and you can
[26:07] see right here that this is going to
[26:08] call a function and it's going to do all
[26:10] kinds of customized things using PHP
[26:13] right inside of your web page okay so
[26:15] that is just a rundown of object
[26:17] oriented programming in general I
[26:19] covered this stuff in my WordPress
[26:20] tutorials like crazy so if you want to
[26:23] see more examples of how all this is set
[26:25] up I have WordPress tutorials that cover
[26:27] it in extreme detail I've about 80
[26:29] videos so all of the code is available
[26:31] in the description please leave your
[26:33] questions and comments below otherwise
[26:35] till next time
⚡ Saved you time reading this? Transcribe any YouTube video for free — no signup needed.