---
title: 'Object Oriented PHP'
source: 'https://youtube.com/watch?v=5YaF8xTmxs4'
video_id: '5YaF8xTmxs4'
date: 2026-06-17
duration_sec: 0
---

# Object Oriented PHP

> Source: [Object Oriented PHP](https://youtube.com/watch?v=5YaF8xTmxs4)

## Summary

This 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.

### Key Points

- **Introduction to OOP** [0:00] — OOP models real-world objects using classes, which define attributes and methods.
- **Inheritance Explained** [1:27] — Inheritance allows a class to inherit attributes and methods from another class.
- **Polymorphism Overview** [2:13] — Polymorphism allows different classes to be treated as instances of a common superclass.
- **Interfaces Defined** [3:11] — Interfaces are contracts that force implementing classes to define certain methods.
- **Access Modifiers** [4:08] — Access modifiers: private (only within class), public (any code), protected (class and subclasses).
- **Static Attributes** [5:20] — Static attributes are shared across all instances of a class.
- **Encapsulation** [6:48] — Encapsulation protects data by restricting direct access and using getters/setters.
- **Constructors and Destructors** [7:59] — Constructors initialize objects; destructors handle cleanup.
- **Magic Getters and Setters** [9:53] — Magic methods like __get and __set provide shortcuts for accessing protected attributes.
- **Method Overriding** [12:32] — Method overriding allows a subclass to redefine a parent method.
- **Final Methods** [16:58] — The 'final' keyword prevents a method from being overridden.
- **__toString Magic Method** [17:53] — The __toString magic method defines how an object is represented as a string.
- **Implementing Interfaces** [18:32] — Interfaces enable polymorphism; a class can implement multiple interfaces.
- **Static Methods** [21:31] — Static methods can be called without instantiating the class.
- **Abstract Classes** [23:56] — Abstract classes cannot be instantiated and force subclasses to implement abstract methods.

## Transcript

well hello Internet and welcome to my
tutorial on objectoriented PHP in this
tutorial I'm basically going to cover
everything you ever want to know about
objectoriented side of PHP and I have a
lot to do so let's get into it okay so
what I'm going to be using in this
tutorial is kodo uh you can use a basic
text editor but I just decided to use
this because it's free and just to give
you another idea what you can develop
PHP with and you can find it at kodo
id.com download slum sign edit and I
also have it in the description now
before we start I thought I would very
quickly cover exactly or at least
roughly what object-oriented programming
means now if we have a basic class man
what that's going to mean is all men
have attributes like the man you'd see
here he might have black hair is H might
be Bob he might have an array of ties
shirts pants and shoes and we also all
have abilities or functions or
operations that we can perform like
every man can talk walk run throw eat
and and work now objectoriented
programming allows us to model Real
World objects inside of things called
classes they are the blueprints for
creating objects and inside of those
classes we're going to define the
attributes and the functions or methods
that those objects can do and that is
the basics of what it means to be a
class and what a class does and what an
object is we model Real World objects in
object-oriented programming that's why
it's so awesome so what is inheritance
well let's say we have an object that is
called man and we want to create a more
specific version of a man called Mark
well what we're going to do is Mark is
going to inherit all the attributes and
functions that are in the man class and
then we can build or change those
attributes and functions in the mark
class and that is basically what
inherence is and overriding just means
that we would go in and say override a
function so that a function doesn't do
what it previously had done now when I
refer to function they are also referred
to as methods and operations but they
are basically functions some people
might disagree with me saying that but
either way just to keep this basic you
can think of methods functions and
operations as being all the same things
so what is polymorphism aside from being
a big word well like we just saw there
mark inherited all the attributes and
all of the functions or methods from the
man class whenever he created the mark
class now if he goes into the walk
method and instead of just printing out
walk he instead says he wants to print
out walk fast whenever Java sees that it
is getting a man object that is trying
to walk it is going to print out walk to
the screen and whenever it sees the mark
class it's going to say oh you inherited
the walk method from the man class we're
going to go in and make certain that you
didn't change the walk method in any way
jav is going to see that we instead want
to walk fast with the mark object and
hence it's automatically going to print
that out now polymorphism can get a lot
more complicated from there but that is
just a basic gist or something just to
understand and you'll understand a lot
of how polymorphism works and that
brings us to interfaces what is an
interface well basically when our class
Mark decides that it wants to implement
an interface what it's in essence doing
is signing a contract that says I the
mark class promise to implement any
methods or functions that are in the
interface and I'm going to show you
examples of all these different things
right now okay so this time I have
Komodo on the left and I have Google
Chrome on the right and I have my server
set up in Local Host and I'm just going
to execute things first thing I'm going
to do is command and create a class well
all you do is type in class and in this
situation I'm going to type animal and
that is going to be it I'm going to make
some changes to it later of course and
then inside of this what I'm going to do
is I'm going to define the attributes
that I want my animal to have well the
very first I'm going to do is I'm going
to type protected in here and there are
different ways to categorize both
methods as well as attributes you can
have protected you can have public and
you can have private now private means
that only methods or functions in the
class can access and change this data
public means that any code can directly
access and change the values for these
attributes and protected is just like
private meaning that only method in the
class can access and change this data
however when we create a subass like we
did Mark from the man the subass is
going to still be able to change the
value for this attribute so I want the
protection of private but I also want
sub classes to be able to inherit all
this data so if it was Private couldn't
do that just stick around here you'll
get it so I'm going to create a couple
protected attributes and let's say that
all our animals are going to have names
and all I'm doing here is to defining
the attributes we have and let's also
say that each of these animals is going
to have a favorite food not favorite
name favorite food protected again
they're all going to make a specific
sound and they are all going to have a
specific ID number this brings us on to
the next thing I want to talk about
which are static attributes and this
time I'm going to make this public so
anybody can access this and let's say
that every time I create a new animal
object I want to automatically increment
the number of animals it's going to
start off at zero because we haven't
made any animals yet and what static
ultimately means is that every object of
the type animal that is ever created
they are all going to share number of
animals so if this number goes up to one
it's going to go up to one for every
other animal object that is created and
if you ever want to access a static
value and make a change to it I'm going
to show you how to do this later you're
just going to type in the class name
with two colons and then number of
animals and then you could go and add
one to this or do whatever I'm going to
actually show you this in an example in
a second another thing we can have
inside of here are constants and there's
a constant and let's just say I want to
put Pi in here for some Bizarro reason
so I'm just going to put in
3.14159 and there we go and a constant
just means that it's going to be
constant and it's never going to change
its value if you want to refer to that
once again you're going to type in the
animal class name or the class last name
two colons and then Pi in this situation
and that's going to get you access to
that and once again I'm going to show
you that in a second now you're going to
Define functions inside of classes just
like you define functions in anything so
let's say I want to create a function
that is going to return the name this is
a situation in which we have something
called encapsulation not that
complicated remember when we talked
about private and protected and public
well by marking these as protected or
private we are encapsulating or protect
ing that data we are saying that we
don't want anybody to come in here and
willy-nilly be able to change the data
what we're going to say instead is if
they want to get the name for my animal
they're going to have to call the get
name function and when they do we're
going to return it and we encapsulate or
protect data so that they can not go in
there and do things like change the name
of my animal to a number or something
nonsensical okay just to protect the
data and as you can see right here if if
we want to refer to an attribute inside
of a class what we do is we type in this
meaning this specific object and we want
the name that is stored for that
specific object this is basically used
as a generic way to say that we want the
name for whatever the current object we
are using because it's not going to be
able to address the object by name so we
just use this now another functions that
is very useful is called a Constructor
and to create that we just type in
construct with two underscores in front
of it and then what this guy is going to
do is initialize things or set some
values whenever we create every animal
well what do you think would be a good
value to change well we're going to need
an ID for our animal that's going to be
random and unique and we're going to
have to increment the number of animals
when we create a new animal object so if
we want to create ourselves a new ID we
want to refer to our ID for our animal
object that we created we just go this
again with that little arrow and then we
go Rand and then if we want to generate
a random number let's say between 100
and 1 million we would just do that and
there we go so now each animal that we
create is going to sort of have a random
number and let's say that we want to
Echo out to the screen the ID this like
that ID and then we'll put some text in
here has been assigned and then we'll
throw a break statement in there at the
end with a semicolon of course and now
I'm going to show you how to access the
static attribute that I talked about
before we're just going to type in
animal two colons and then we're going
to type in number of animals and we are
going to increment it because we created
a new animal object another thing we
have available to us is what's called a
Destructor and let's just put public in
front of that and two underscores and
destruct now the destructor is going to
be called when all references to an
object have been unset so if there's
some housekeeping you want to do
whenever an object is deleted you could
do so in this situation what I'm going
to do is I'm just going to print out to
the screen that the object has been
deleted is being destroyed of course you
would never use this but in your uh
website but whatever put a little frowny
face there and there we go now remember
up here where we were creating the git
name function so that we could get the
value of name for our different
functions well PHP actually provides a
shortcut way to be able to create what
are called Getters and Setters Getters
are going to get you the value of these
attributes and Setters are going to set
the values and because they're protected
that means we need to use a function to
change these values and get these
attributes and these little handy dandy
things are called Magic Setters and
Getters and these are actually magic
methods as well anything with an
underscore is a magic method so if we
want to define a function inside of this
guy that is going to be able to get
anything we're going to type in function
and then we're going to type in get and
we're going to pass the name of the
attribute that we want to get to this
function whenever ever it's called and
if we want to print something out to the
screen just to show that something's
going on here we could do something like
asked for name which is going to be the
attribute they're asking for and a break
statement again and then if we want to
return it the attribute we can just come
in here and go this and the name that
was passed inside of here so we're going
to have to put a dollar sign and name
because it would get confused otherwise
it would think this was the name we're
looking for this is going to return
whatever attribute is passed in here and
I'm going to show you examples of all
these here in a second now if we want to
set the value this time I'm going to do
something a little bit more complicated
just to do something a little bit more
complicated I'm going to check that the
attribute that they sent in here was
actually a valid attribute now with this
you're going to pass in the attribute
name and the new value that you want to
set for set attribute and let's say I
want to check that the name they passed
in is actually a valid attribute one
thing I could do is use a switch
statement here and then I can come in
here and go case and I'm just like I
said I'm just doing this to show you how
to do it and then we could come in here
and say if it's name well we know name
exists this so set name to whatever the
value is and then we can do pretty much
the same thing well we're going to throw
in a break statement also there we go
and we could do the same thing with
favorite food do the same thing with
sound once to set the sound for my
animal and then finally if it's none of
those attributes what I'm going to say
is I'm just going to Echo out to the
screen name not found and then after all
that's done I get out of my switch
statement I could come down here and
just to print something out on the
screen I could print out set name name
to Value so there that is and that is
how the little magic methods for setting
and getting are used and like I said
before inheritance occurs whenever you
create a new class by extending another
one and like I said we're going to
inherit all the attributes and methods
that are defined in the super class
animal in this situation is a super
class inside of the subass so what we're
going to do is we're going to create a
new class and it's going to be called
dog and if we want to inherit everything
from animal we're going to type in
extends and then animal and later on
I'll cover interfaces now let's jump up
here and let's create a function that we
are just going to override so we'll
create a function called run inside of
our animal class we're in the animal
class right now and let's say this one
goes Echo this name runs we'll throw a
braak statement in there and then we'll
actually just copy this cuz we're going
to override it and as you can tell I
don't need to put the attributes inside
a dog that are an animal they're
automatically there so name is inside of
this and let's say that our dogs run
like crazy and that's how you override a
method so now what I'm going to do is
I'm going to jump outside of the dog and
animal class and start creating them and
you can watch everything sort of execute
here over inside of chrome so I'm
outside of both of the classes and if I
want to define a new animal I just go
animal and let's say I want to say
animal one is equal to new animal and
there we go and the Constructor this guy
right here is going to be called
whenever I create this new new animal
and I could also pass in attributes for
the Constructor but I decided not to do
that now what I'm going to do is come in
and set the attributes for my new animal
object I created and I'm just going to
type in name and I'm going to give it
the name of spot and then I'm also going
to set favorite food equal to meat and
then I'm going to set the sound that my
animal makes to rough now if I want to
print out all this new data onto our
screen I could do something like Echo
animal one name
says and then call the sound for my
animal and then go down to the next line
and we'll say something like give me
some and then let's say favorite food
and then let's say we also want to print
out the ID number here my ID is put
another period and all the code is of
course available in a link in the
description and then if I want to print
out the ID I just go ID and then if I
also want to print out the total animals
that have been created go total animals
is equal to and then if I want to call
that stack IC value number of animals
remember I just type in the class Name
colon colon number of animals put
another period inside of there throw two
break statements in there and there we
go now I can go over inside of chrome
and execute this and you're going to see
that the ID number has been assigned the
name of spot has been assigned Favorite
Foods meat sound is rough I called the
getter to get the name the sound and the
favorite food and then it printed out
all of that information on the screen
and then you can also see here that spot
was destroyed after the object was no
longer longer needed this is the
destructor function right there if I
wanted to come in here and also print
out the favorite number just to show you
how to print a constant from a class
favorite number again just type in the
class name and piie and we'll throw in a
break statement and there you can see
favorite number printed out as well now
I'm going to do exactly the same thing
except I'm going to use the dog class so
I'm going to come in here and just copy
all this because there's not much I need
to change paste that in there and I'm
going change this to animal 2 and it's
going to be of type dog change that that
to two change that to two change that to
two two two two two and I can change
this to dog and it's still going to be
able to get it and then I'm just going
to come in here and change the
attributes I'm going to change this to
Grover I'm going to change this to
mushrooms just to be weird and I'm going
to change this to G save it over here
execute and there you can see we created
a new ID number the name is set to
Grover all those things are working
Grover says G give me some mushrooms my
ID is blah blah blah blah blah one thing
you might find weird here is animals
shows up as two there are still two
animals created and even though we are
referring to a dog in this situation it
is still going to increment the numbers
because it ultimately calls the
Constructor inside of the animal class
now the next thing to talk about here is
we went in here and we overrode the Run
class so here we have runs for our
animal class and here we have runs like
crazy for a dogs class so I'm going to
show you exactly how that works Works
come in here and just go animal one and
if we want to run a method inside of
here we just type in whatever the
object's name is and then whatever we
want to run and animal two in this
situation and run and when we execute
you're going to see spot runs and Grover
runs like crazy and you can also see
here Grover is destroyed spot is
destroyed now what would happen if we
want to actually come into our animal
class and Define a function that we
don't want to allow the subclasses to
override well we're in the animal class
right now it's actually quite easy we
just type in final and then if they try
to override it in the dog class well
it's just going to throw up a whole
bunch of errors it's not going to allow
it to happen so let's say we create a
function called what is good and all it
says is running is good and once again
if we wanted to execute this we would
have to come in here and just say what
is good again we're going to use the
same whatever the object's name is well
we could just do this what is good and
you can see running as good as right
there so even though it's just an animal
of course it's going to run inside of
the dog class CU it inherited that
another thing we could do is you see
here where we're printing out all this
information well let's say we wanted
this information to automatically be
printed out anytime the user said Echo
and Then followed by the name of our
object well we could do that what we're
going to do is we're just going to come
in here and copy this and then we're
going to go up into our animal class we
going go right here and we're going to
use another magic method and this one's
going to be called two string and all
two string does is it defines inside of
here what happened happens whenever the
user says I want to print some
information out on the screen whenever
somebody goes Echo and follows that up
with the name of the object all you're
going to do is type in return and then
you're going to print out what you want
to show up except you're going to want
to change all of these specific object
names to this so just change these to
this this this and then here we're going
to change this to animal of course and
everything else can stay the same and
then you could do something like go Echo
animal one just type in the object's
name save it execute there you can see
it printed out on the screen right there
now remember when I talked about
interfaces and interfaces are just a
contract that says that any class that
implements an interface must Define the
functions that are inside of the
interface and this is a way for us to
take advantage of polymorphism so we're
going to do here is Define an interface
by going in typing interface and let's
say we want to say singable we want to
give our animals the ability to sing and
we're going to come in here and Define
the function but we don't say what the
function does we just leave it like that
and what that means is when we go into
say our dog class and say implements and
then singable that it is going to force
us to Define sing inside of it so we're
going to say function sing and if you
don't Define anything a function is
automatically public and then we're
going to say Echo this name sings and
then we'll do something like g g g g
semicolon at the end and then we're
going to do exactly the same thing with
our animal CL class so come up here and
the great thing about using interfaces
is you can only ever extend one object
but you can Implement a NeverEnding
array of interfaces so that's why they
are very useful so singable it's going
to say we have to create the sing class
inside here or the sing method there we
go and there we are and then just Bow
Wow Wow and then if we come down here
and go animal one or animal 2 or
whatever and call sing it is going to
print that out on our screen so there's
an example using an interface another
example of polymorphism is here where
we're going to be able to Define
functions that are going to be able to
accept classes that extend a specific
class or a specific interface here's
polymorphism in action again so this is
outside of any of the classes we're
going to come in here and we're going to
type in function and we could say
something like make them sing and then
we could say singable because both the
animal and the dog objects are both of
type singable and we could just type in
singing animal here and then we could
just type in singing animal and call
sing and it's automatically going to
execute the right sing in the right
animal even though it doesn't say here
that it's of animal type or of dog type
it says it's of singable type so we'll
be able to just come in here and go make
them sing and pass in animal one or
animal 2 and when we execute it's going
to automatically have the right animal
sing see there you go and of course
since you can do this with the different
classes that implement the different
interface of course you're also going to
be able to do it with the dog class
because it extends the animal class so
here what we're going to do is go
function sing animal and you can see
there it's animal type singing animal
once again this is outside of any of the
classes and I'm just going to copy this
up here so I don't have to type it out
again and once again I'm going to be
able to go sing animal sing animal and
then let's just copy these there you go
execute and you can see it prints out
the right things even though all I said
here was that I was passing animal
objects okay polymorph is in action
another type of function you're going to
have inside of PHP is what are called
Static functions now static functions or
methods can be called without the need
to instantiate or create an object
sounds like a big word but they're not
really that's what it means when
somebody says instantiate that means
they just created an object let's just
go into the animal class again and I'm
going to type in static meaning this
belongs to the class add these and let's
say that I wanted to accept a number one
and a number two as like a utility
function that doesn't really make sense
that my animals will be able to add but
I wanted to have this in here for some
Bizarro reason this is terrible form by
the way but I'm just doing this just to
show you what a static function is
number one plus number two throw a break
statement in there and now what I can do
is come down inside of here and do
something like Echo 3 + 5 is equal to
and if I want to call that static
function I just go animal which is where
it resides and then I call add these and
then let's say I pass in three and five
and then if I execute it you're going to
see 3 + 5 is equal 8 that's a static
function and now I'll run through a
bunch of other different things you can
do with PHP you can check the class type
so let's say something like is it an
animal we want to check if it's an
animal you would go and put something
like animal to and then instance of and
then it'll say animal and yes indeed
animal 2 even though it's a dog object
is an instance of the animal object type
and then we'll say that we want to
assign true if it is or false if it
isn't and you could go in here and put
other different things inside of here
and check the class type on your own
Echo do something like it is that animal
I'm using single quotes here so I can
actually type in animal 2 in this
situation with the dollar sign is an
animal break statement it is true that
animal 2 is an animal see print it right
there another thing we could do is clone
which is make a clone of an object and
you could actually inside of your class
Define the things that you want to be
copied over to any clones and you would
just create a function inside of it with
the magic two underscores with clone
after it and then that would be called
anytime something was cloned but just to
keep this nice and short I'm just going
to show you how to create a clone so
let's go animal clone is equal to and
then you would just type clone and then
you'd go animal one now we just create a
copy of it another thing you can do is
Define what are called abstract classes
and and also abstract methods now an
abstract class cannot be instantiated it
cannot have an object be created of type
whatever the abstract class is but
instead it forces classes that implement
it to override every abstract method
that exists inside of it you want to
create an abstract class you just type
in abstract class and then whatever you
want it to be called and then inside of
it you're going to Define all of the
different methods or functions that must
be used by any class that implements
this abstract class so it's just random
function and it could have an attribute
in it or it could have whatever so
there's an abstract class and then you
could also use something that I almost
never use which is call inside of a
class and if it is defined inside of a
class what this is going to do is
provide method overloading but since PHP
isn't strongly typed there's really no
reason to use it now I just wanted to
briefly give you an idea that I mean I
covered pretty much everything but want
to give you an idea of how this is used
and how this is used in things like well
I'm going to show you this this is very
simple let's say I wanted to create a
way to very quickly be able to or very
easily be able to define the title
description keywords an author for my
website and also provide ways to get
those titles and also provide ways to
create the opening part of my HTML
document and like I said I'm dumbing
this down a lot okay so I would go in
and Define all these functions and all
this information I then then inside of
my website could just do this I would
say require and site info.in is the name
of this file right here I could just
come in here and very quickly whip out a
site okay now things normally aren't
done that way because you're normally
pulling data from databases and setting
things up right so what I decided to do
is go into WordPress and I did a ton of
tutorials and this is how WordPress and
every customer management system works
this is it so you can see here I'm
including data that I want to use or
functions I want to execute and you can
see right here I have things like gith
header this is WordPress and this is
automatically going to pull that
information from this file and you can
see right here that this is going to
call a function and it's going to do all
kinds of customized things using PHP
right inside of your web page okay so
that is just a rundown of object
oriented programming in general I
covered this stuff in my WordPress
tutorials like crazy so if you want to
see more examples of how all this is set
up I have WordPress tutorials that cover
it in extreme detail I've about 80
videos so all of the code is available
in the description please leave your
questions and comments below otherwise
till next time
