---
title: 'PHP OOP -  Beginners Crash Course'
source: 'https://youtube.com/watch?v=dQxuYRNbL_M'
video_id: 'dQxuYRNbL_M'
date: 2026-06-30
duration_sec: 1445
---

# PHP OOP -  Beginners Crash Course

> Source: [PHP OOP -  Beginners Crash Course](https://youtube.com/watch?v=dQxuYRNbL_M)

## Summary

This video provides a beginner-friendly crash course on Object-Oriented PHP, covering fundamental concepts like classes, properties, methods, access modifiers, constructors, inheritance, abstract classes, and static members. The presenter assumes viewers have basic PHP knowledge or experience with other OOP languages, and demonstrates each concept with simple code examples.

### Key Points

- **Defining a Class** [1:03] — A class is defined using the `class` keyword followed by the class name (e.g., `Customer`). It serves as the building block of OOP in PHP.
- **Properties and Methods** [1:35] — Classes contain properties (attributes) and methods (functions). Properties are declared with access modifiers (`public`, `private`, `protected`). Methods are functions inside a class.
- **Access Modifiers** [2:11] — Three access modifiers: `public` (accessible from anywhere), `private` (accessible only within the class), and `protected` (accessible within the class and its subclasses).
- **Instantiating an Object** [3:36] — Create an object using `new ClassName()`. Access public properties with `$object->property` and call methods with `$object->method()`.
- **Constructor and Destructor** [7:34] — The `__construct()` method is called automatically when an object is instantiated. `__destruct()` runs when the object is destroyed. These are magic methods.
- **Inheritance with `extends`** [13:47] — A class can extend another class using `extends`. The child class inherits properties and methods from the parent. Use `parent::__construct()` to call the parent constructor.
- **Abstract Classes and Methods** [17:54] — An abstract class cannot be instantiated directly. Abstract methods have no body and must be defined in child classes. Declare with `abstract class` or `abstract function`.
- **Static Properties and Methods** [20:35] — Static members belong to the class, not instances. Use `self::` to access them inside the class. Call externally with `ClassName::method()` (e.g., `User::getPasswordLength()`).

### Conclusion

This crash course covers the essential OOP concepts in PHP—classes, properties, methods, access modifiers, constructors, inheritance, abstract classes, and static members—providing a solid foundation for building object-oriented applications.

## Transcript

hey guys this video is going to be on
object orientated PHP all right so if
you do not know any PHP at all I would
suggest that you at least learn the the
very Basics uh at least things like
variables arrays functions Loops just
just the the the real fundamentals of
PHP all right uh if you if you don't
know PHP but you have experience in
another language especially if it it's
if it's an object orientated language
then you should be all right all right
uh what what we're going to be doing is
going to be pretty simple so if you
already know uh object-orientated PHP
then you probably don't want to continue
because it's it's probably just stuff
you already know um but feel free to
watch and and maybe use it as a
refresher all right so what I have is I
already have my server set up I'm using
zamp or xamp and I have my local host at
local. and then I just have a index PHP
file so the first thing we're going to
go over is classes okay classes are the
basically the building block of
object-oriented programming at least in
PHP all right so to define a class you
just want to use the word class and then
whatever you want to call it in this
case I'm going to call it customer all
right and I haven't already planned out
this code just to let you know I'm just
kind of uh freestyling it all right so
we may have a couple errors or or
whatever but um it'll be fine all right
so we have a class called customer and
then in a class you have two core
components that's properties and methods
properties are basically just attributes
of the class all right so we have a
customer class so we may have something
like um an ID let's say uh a
name
email um I D name
email and let's
say
location okay now when we're using PHP 5
or newer which most likely you are you
want to you want to use what's called an
axis modifier all right also sometimes
called an axis identifier and there's
three different ones you can use okay
there public which means that and I'm
going to go over this more in depth in a
little bit but public just means that
you can access this this property from
outside of this class all right we also
have private okay that's going to make
it so you can't access this anywhere
except this class okay you can't access
it outside of it you can't access it
from another class even if it's extended
from this one all right and then you
have
protected which means that you can't
access this from
outside but you can access it from
another class class that extends
customer all right so for example if we
have let's say
class um my
class
extends customer okay if we do that then
we'll be able to access email from in
that class all right so hopefully that
makes sense uh but what I'm going to do
is I'm going to set all these to public
for
now okay and we'll get location
all right so now we have some properties
for our customer
class actually you know what instead of
location let's use
balance okay since they're customers
they probably have some kind of
balance now we're just declaring these
declaring these but you can also set a
default okay if we wanted to say ID
equals 1 we could certainly do
that okay
and next thing is methods methods are
functions so try not to get confused
with methods and functions all a method
is is a function that's inside of a
class all right so you can also use uh
access modifiers on that as well so
we'll say public function and let's
say uh get
customer all right now this most likely
would take in an
ID okay so when you call this you would
you would put the customer ID in
and then most likely what you would do
is you would take the ID that's passed
in and you would assign it to the
property ID so to do that what we would
do is say
this ID equals ID all right so if you
ever want to access a property in your
class you could use the the keyword this
and then you want that and then ID all
right or whatever the property is all
right that way you can access these from
every method in your class
all right because if we just keep it as
ID and get customer we we can't access
this from anywhere
else now uh a method like get customer
what it would probably do in a real
application is it would go and fetch a
customer from the database with that
particular ID all right and then you
would probably just return the customer
so we don't have the database part but
what we can do is just say return and
let's say it grabs a user called John do
all right our customer called John
do so now I'm going to show you how we
can instantiate this class inside of our
PHP all right so we want to make sure
that we go outside of the class now all
right now to instantiate it you're going
to create a variable let's say customer
and we want to set it to New and then
the name of the class so new customer
all right and that gives us a new
customer object or instance all right
right and then we can continue and we
can actually access public properties
and and public methods or functions all
right so for instance uh let's say that
we want ID to be one by default then we
can go and we can
Echo customer
ID save it reload and we get one now
just to demonstrate these uh the
visibility or the axis modifiers here
let's make this
private okay and if we go and reload
we're going to get this cannot access
private property okay if I change it to
protected then it's saying cannot access
protected property all right so let's
make it back to public and get rid of
that okay now if we want to call a
method we can do the same thing we're
going to Echo customer and let's say get
customer and then we need to pass in an
ID
all right uh so if we go ahead and
reload we get John Doe because that's
what we're returning here all right if
we want to return the ID we could do
that okay so let's pass in
11 and reload and we get 11 all right
and then the same thing if I set this to
private we get the error okay same thing
with
protected all right so that takes care
of properties and methods now we also
have a method called construct okay
which is a Constructor and there's a lot
of different programming languages that
use Constructors what it is is it's a
special function that or method that is
going to run when we instantiate the
object so when we run this if we have a
construct function that's going to go
ahead and run all right so let's create
one we'll say public function and then
you want to use double underscore and
then
construct okay just like that and just
to test it out we'll say
Echo uh we'll say
the
Constructor
ran all right so now let's save and all
we're doing here is instantiating the
customer object okay and you can see the
Constructor ran
and we also have another one and these
are called Magic methods okay and
there's a bunch of them I'm going to go
over some more in a little bit but
construct and and um destruct are magic
methods all right so let's say public
function
destruct and uh we'll do the same thing
we'll just
Echo we'll say we'll say destruct
uh Destructor
ran all right so if we reload you can
see we get the Constructor ran and then
the destructor ran all right and if we
go ahead and spit out the customer again
let's actually just return the
name okay when we run this it should go
in between the construct and destruct
functions all right so uh let's say
custom
customer
get customer and just put in 10 okay now
if we
reload uh oops we got to Echo
it okay so now you can see that John Doe
is being printed out in between the
two now common practice would be if
we're going to just get a customer here
and we're going to pass in um an
attribute or property and then assign it
like this we can do that in the
construct all right so it would take
away the need to have this really all
right so um unless you're dealing with
the database and all that you would do
it but if we're just assigning uh class
properties then you want to do that in
the construct all right so in here let's
say
ID uh
name
email and balance okay and then Let's uh
let's copy that and we'll just paste
that
here okay so we get ID then we'll do
name this will be
email and
balance all right and I like to make
these nice and neat
okay and then we can get rid of this all
together okay and we don't need this
destruct
either and we'll get rid of this all
right so what we can do now is when we
instantiate the customer we want to
actually pass in the values here all
right so the ID let's say
one then the name we'll say my
name and then an email Ma say Brad at
Gmail and then a balance let's just say
zero okay so if we want to Output any of
these we can now just say
Echo
customer and let's Echo out the
name okay and that's going to give us
the name so now we have this customer
that we can work with if we wanted to
create a function
[Music]
function called let's say um get
email all right and then we can
just
[Music]
return
this email all right and then instead of
doing this well let's get rid of that
and say we want the
email and that gives us the email and
notice we don't have to pass anything in
here we don't have to pass in an ID or
anything because we have our customer
right here it's an instance it's just
talking about this one customer all
right so we can continue to uh run
methods on that
customer now the reason that you would
want to do this is because uh you could
keep your properties all private and
then you could just have a method that
gets that private property okay so let's
make all these
private and this is really common
practice okay so if we go ahead and save
that reload we're not getting any error
and it's letting us get the email
address because we're not accessing it
directly okay we're not saying customer
email okay if we do
that we're going to get that error okay
because what we're doing is we're using
this method here to get the private
property all right and and this is
public I mean if I set this to let's say
protected and reload we're going to get
an error okay so public methods private
properties is uh is really good practice
so let's put that back all right so now
let's talk a little bit about
inheritance okay so as I said you can
have a class extend another class so
what I'll do is let's um
let's comment this out for now and we're
going to create another
class
um so let's say class and normally you
would have these in their own files okay
you'd have a customer for instance a
customer. PHP and then um whatever else
you have for classes in this case let's
call it this uh
subscriber okay so subscriber is going
to extend customer so we want to say
extends okay make sure it's plural
extends
customer okay so now we have two classes
now in
subscriber let's create its own property
okay so we'll say
public and let's say there's a property
called plan okay this is a subscriber
and they're on a specific plan okay and
plan is not part of Customer because not
all customers are
subscribers now when we instantiate a
subscriber we want to do the same thing
that we we did with customer it we want
to pass these in all right but we don't
want to
repeat um having to put all these down
here and these um so what we'll do is
let's create our construct I'm going to
copy this
one all right now we're going to have to
pass in all the stuff ID name email
balance
but we don't want to have to do this
again all right so let's get rid of that
and what we can do here is we can
specify that we want the parent
construct to run okay so to do that
we're going to say parent and then a
double colon and
then
construct okay just like that and then
we want to pass these
in all right now when we instantiate a
subscriber we also want to put in the
plan all right so we want to make sure
we put that here but we don't want to
pass it to the parent because there is
no plan up here okay it's not a um it's
not a customer property it's a
subscriber property so what we'll do is
down here we'll say this plan is going
to be equal to the plan that's passed in
all right and that'll take care of that
and now what we can do
is let's take the get
email and cut that
out and we'll paste it in here
here all right so now we have subscriber
get email and we want to make sure that
the email up here in the properties is
set
to protected okay because that means we
won't be able to access it from outside
unless it's it's a class that extends
customer okay so now what we can do is
copy that make sure you're outside of
both classes and this is going to be now
subscriber he's going to equal new
subscriber and now we can actually pass
in the plan as well okay so we'll say
Pro all right and then we should be able
to call get
email so
subscriber and we'll say get
email actually we need to Echo it
out all right and now now we can get our
email okay through
subscriber now if we wanted the get
email to actually stay in the customer
class we can do that because since
subscriber extends customer we have
access to this all right so we can
actually save that and that's going to
do the same thing all right now next
thing I want to talk about is abstract
classes all right and abstract methods
now when you have an abstract class it
can't be instantiated okay so if we make
customer abstract we can't do this down
here we have to just use classes that
extend it all right so let's take a look
at that so we'll
say abstract class customer all right
and just to show you what it does let's
comment this stuff out and then we'll
uncomment that and reload and you get
cannot instantiate abstract class
customer
okay so let's comment that back
out and then let's uncomment this and
let's try to call get
email all right
so that's going to work but if we want
to make let's I just want to show you if
we take away the abstract in the class
and add it
here onto a method and then we try it's
telling us that get
email uh can't be run and the reason for
that is when you have an abstract method
like this it needs to be in an abstract
class okay so we need to put that back
here okay and then
reload uh wait a minute
here Echo get subscriber
email cannot contain
body um
oh that's because when we have our
function here we can't actually um
include a body all right so what we'll
have to do is copy that and then this is
we're just going to declare it like that
all right and then down here is where we
want
to call it like
that all right so you just want to
Define your fun functions in the
abstract class okay it's basically you
can think of it as a base class that
that you don't use directly but you
extend uh classes from
it all right so hopefully that makes
sense um in my experience I haven't
really needed to use abstract classes
but um it's it's it's good to know it's
a fundamental part of um object-oriented
program all right so this is starting to
get a little cluttered so I'm going to
just get rid of all
this and what I want to move on to now
is the static keyword all right so you
can set static properties and methods
there you don't Define the class as
static but the properties and methods
inside of it you can all right so let's
create a
class
um let's call it
user and let's say user has just a
regular uh property of
username and
also
password okay now the purpose of using
static classes and properties is that
you don't have to create an instance you
don't have to
instantiate uh when you do that all
right so let me just give you an example
and we're going to say public static
and let's say password
length Okay and we're going to set that
to a default of five all right so
password length would be a good thing to
to make static because you don't need a
certain instance it's always going to be
the same the username and password are
going to be different depending on the
user um but password length is going to
be the same no matter what so that's a
good example to use a static property
all right and then we want to create
let's say
public um public
static function and we'll say
get password
length all right and what we want to do
is return now when you're dealing with
static properties you you don't want to
use this you don't want to do that what
you want to use is the keyword self and
then you want to use this double colon
and and then the property okay so in
this case it's going to be password
length Okay so what we want to do now is
let's go outside of the user outside of
the user class and to call this we don't
need to say um you know user equals new
user or anything like that what we can
do is just the name of the class and
actually let's Echo out
user and then same thing you want to use
this double colon and then the function
which is going to be
get password
length okay just like that let's go
ahead and reload and it's giving us
five all right and you would probably
have this private but if it's public and
you want to access it
directly you can do that as
well okay of course if I said private
it's going to give us an error all right
so that's how static uh static
properties and static methods work all
right so that's going to be it for now
those are the the the very basic
fundamentals that you need to know when
working with object orientated PHP all
right I will hopefully create a part two
of this where we can actually create an
application that's going to that's going
to use um these Concepts all right so
for now thank thanks for watching please
subscribe leave a like leave a dislike
if you didn't like it and I will see you
next time
