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