---
title: 'OOP In PHP | Object Oriented Programming In PHP | PHP Tutorial For Beginners | Simplilearn'
source: 'https://youtube.com/watch?v=JSX0HMYgtvc'
video_id: 'JSX0HMYgtvc'
date: 2026-07-28
duration_sec: 1654
---

# OOP In PHP | Object Oriented Programming In PHP | PHP Tutorial For Beginners | Simplilearn

> Source: [OOP In PHP | Object Oriented Programming In PHP | PHP Tutorial For Beginners | Simplilearn](https://youtube.com/watch?v=JSX0HMYgtvc)

## Summary

This video provides a comprehensive overview of Object-Oriented Programming (OOP) concepts in PHP, including classes, objects, constructors, destructors, inheritance, and polymorphism. It also includes live coding demonstrations to illustrate each concept.

### Key Points

- **Introduction to PHP and OOP** [0:08] — PHP is a server-side scripting language used for developing dynamic websites. The video covers eight OOP concepts: class, object, member function, member variable, constructor, destructor, inheritance, and polymorphism.
- **Class Definition** [1:46] — A class is a blueprint for an object, containing properties (variables) and methods (functions). Example: class Fruit with properties name and color, and methods setName and getName.
- **Creating Objects** [5:19] — Objects are instances of a class created using the 'new' keyword. Multiple objects can be created from the same class, each with different property values.
- **Constructor and Destructor** [9:47] — A constructor is a function called automatically when an object is created, used to initialize properties. A destructor is called when the object is destroyed or script ends.
- **Inheritance** [17:00] — Inheritance allows a child class to derive properties and methods from a parent class using the 'extends' keyword. Only public methods are inherited.
- **Polymorphism** [22:18] — Polymorphism allows methods to be overridden in derived classes. The same function name can be used for different purposes, enabling method overriding.

### Conclusion

The video successfully demonstrates core OOP concepts in PHP with practical examples, making it a valuable resource for beginners looking to understand and implement OOP in their PHP projects.

## Transcript

hello everyone i am anish from simply
learn and welcome to the video on object
oriented programming in php
in this video we will discuss everything
about oops concept in detail
but before that let me tell you
according to indeed.com the demand for
php developers has massively increased
to 835 percent since january 2020
however in today's market it is the
fastest growing tech job across the
industry
so in today's video we'll be discussing
what is php what are the concepts of
oops in php and finally we will have a
look at the demo where we will be
executing the code that has class
objects inheritance and polymorphism in
it
now comes the important question what
exactly is php
so we have spoken a lot about it so far
but do you know what it means
well php stands for hypertext
preprocessor
it is a server-side scripting language
that is embedded into html and used for
developing dynamic websites
now let's move ahead and understand the
concepts of oops in php
so as you can see on the screen i have
listed down the eight concepts of oops
that we will be discussing today
first is class
next comes objects then its member
function and member variables then
constructor and destructor
inheritance and finally it is
polymorphism
now let's walk through each of the
concepts one by one
first is class
it is a blueprint of an object that
provides initial values for the state a
class consists of both data and
functions and data and functions
together are called objects
now let's first have a look at the class
let me create a file
named class dot php
and let us write the code first
um
okay
here i have defined a class by using the
class keyword
followed by fruit which is the class
name
next i have added a pair of curly braces
and added the entire properties and
methods within the braces so as you can
see on the screen we have declared a
class named fruit
that contains two properties called name
and color
along with that i have also added two
methods set name and get name
now now the purpose of these methods is
for setting and getting the name
property
so let me give you some in important
information so when you define a class a
variable in it is called property while
a function is known as method
now let us execute the code and see the
output
let's go to the browser
and type localhost followed by the file
name
here here's a file class.php
and as you can see on the screen
the code is running perfectly
now that was about the class
next comes objects
so it is an instance of a class
basically
a variable holds a date of a class
now we define a class once
and create as many objects in it
for a better understanding let me give
you an example
suppose a car is a class so
mercedes-benz and bmw would be objects
now let's see objects
classes are incomplete without objects
so we can add as many objects as we want
from a class
and every single object has the
properties and methods defined in the
class but those objects will have
different property values
so objects of a class are created using
the new keyword
now let me open the code editor
and let's create another file
called
obj.php
so this code will be the same
let's copy
and paste it here
now let me type the code for object
[Music]
as you can see i have used the new
keyword to create object of the class
fruit
similarly
banana
would be another object
and now we'll use the set name function
to set
the name to apple
on this object on the apple object that
we have created
similarly
we will
set the name
for the banana object
there's an extra bracket here let's move
it
now moving forward let us type the code
to display
what we have added into the objects
for this can you guess which method will
be using
yes the get name method
will be used to display
the name saved onto the object using the
set name method
now that our code is complete
let us run and check
if it's working correctly
let me go back
and refresh
now here we have our object.php file
yes the code runs perfectly the fruit
program and it is displaying apple and
banana from the two objects that we
created i hope now you have clearly
understood the concept
moving on to the next one let's talk
about the member variable and member
function
a member variable is defined within a
class
the data can be accessed by the member
functions alone
now once the object is built these
variables are called attributes of the
object whereas member function is
defined within a class and is used to
access object data
if getting your learning started is half
the battle what if you could do that for
free
visit skill up by simply learn click on
the link in the description to know more
so next comes constructor
so it allows an individual to alert an
object's properties
while the object creation
while a destructor is a function that is
called when the object stops working
we will have a look at the constructor
program
so constructor allows an individual to
alert an object's properties while the
object creation
now suppose you build a program using
constructor function
php will automatically call the
construct function while creating an
object from a class also remember that
when you write the construct function
always prefix it with two underscores
now let's have a look at the example
where we will be using a constructor
function with calling the set name
method
now this set name function is often used
when you assign a value to the name
attribute
now let's open the code editor and
create a constructor file
[Music]
we'll again create the two properties
named
name
and color
now we will create a constructor
with two underscores
and a construct keyword
and i'll give the two properties as
attributes to the constructor
now in this
program we'll be using the constructor
function
as the set
method
to set the names
and color of a fruit by default
during the object creation itself
we still need to create the get
functions
so
now let's create an object to invoke the
constructor
now since the constructor has attributes
we will have to give the values for the
attributes as well
so the fruit would be
strawberry
and
color
would be pink
so this creates a new object and it
gives
the values to the constructor
uh
in the php script
and
our html code as well
let us save the code and check if it's
working
let's go back
refresh
now constructor file is visible let's
click on it and yes
so
when the object strawberry is created
the attributes we have given to the
constructor strawberry and the color
pink were already
assigned to it
and it is displayed in the browser
itself
now that the constructor program is done
next let's see an example of how a
destructor works
let's open the code editor
create another file
destructor.php
now the destructor code will be very
similar to the constructor one so we
will copy the code
into the destructor file and make some
changes
so
let us remove this we don't need it
and write our destructor function
so destructive function is written
similarly
how constructor function is written
so it starts with two underscores and a
destruct keyword
it does not have an attribute
now why did we delete the get functions
is because
we will be using the destructor
for it
also we won't be needing this
just the object creation is sufficient
now let's save our program and
do the drill again
open the browser
here a destructor file is visible
and it shows the fruit strawberry and
the color is pink so when the object
function is complete
and the program ends the destructor
function is called which displays the
strawberry and the color pink
now
comes inheritance so it derives new
classes or child classes from the parent
class
however inheritance can have its own
properties and methods too
so inheritance has two classes
first is parent class and the other one
is a child class
now let's talk about each of them in
detail
so parent class is otherwise called a
base class that is inherited from
another class
and a child class is a subclass that
inherits from another class
a child class can have subclasses and
derived classes
now let's have a look at the inheritance
program
so the child class is derived from all
the properties and methods from the
subclass
in addition it can have its own
properties and methods too
so an inherited class is defined by
using the extends keyword
let's look at an example
open the code editor so i have taken the
liberty to create the inheritance.php
file
and write some of the code that we have
already written so i'll be writing only
the code that is necessary for the
inheritance part
now also remember when you derive a
child class from a parent class only the
public methods are derived into the
child class
so we will be creating the methods as
public
in order them to be
inherited
made this function public
let's
end this class
and create another class in order
to inherit this class
so we will name
another class as
cherry
extends
fruit
and we'll create another function for
cherry so this function is exclusive to
cherry
so we have given a message through this
method
now
let's create an object
cherry
new
cherry
and
give the values for the attributes
of
this constructor
let's save the code
and check if it's working
well there seems to be an error
now let's try to resolve it
okay got it so we
created the object
within
the cherry class so it got localized
what we need to do is put that code
outside of the cherry class
and it should work fine now
yes so is cherry a fruit or a berry a
cherry is a fruit and the color of the
fruit is red
and now we will talk about the last
concept that is polymorphism so
polymorphism has many forms
the same function is utilized for
different purposes
it has a class with a variety of
functions simultaneously sharing a
common interface
so inherited methods are overridden by
redefining the methods in the derived
class now let me write the code to make
it clear
so already created the polymorphism php
file
i have copied the inheritance code
into the polymorphism part
and then now we'll make the changes into
the derived class that is cherry
so let's create a property named
wait
now to
override the constructor and the intro
function
from the fruit class we will have to
create the same functions
in the derived class also
now this function construct has got one
extra attribute in comparison to this
function construct
okay
let's create the intro function too
now a code is ready
let us create another object
to invoke
the functions
now did you notice what we did here
so
when we created the object cherry for
for the cherry class we gave three
attribute values instead of two
in order to invoke this construct
function
not this
let us run it and check
so it runs perfectly it displays the
fruit the color and the weight
now we have reached the end part of the
video on the object-oriented programming
in php
i hope it was informative and
interesting and if you have any queries
about the topics covered in this video
please ask away in the comment section
below our experts are 24 7 ready to
solve your queries thanks for watching
stay safe and keep learning
hi there if you like this video
subscribe to the simply learn youtube
channel and click here to watch similar
videos to nerd up and get certified
click here
