[0:08] hello everyone i am anish from simply [0:10] learn and welcome to the video on object [0:13] oriented programming in php [0:15] in this video we will discuss everything [0:18] about oops concept in detail [0:20] but before that let me tell you [0:23] according to indeed.com the demand for [0:25] php developers has massively increased [0:28] to 835 percent since january 2020 [0:33] however in today's market it is the [0:35] fastest growing tech job across the [0:37] industry [0:39] so in today's video we'll be discussing [0:42] what is php what are the concepts of [0:44] oops in php and finally we will have a [0:47] look at the demo where we will be [0:49] executing the code that has class [0:51] objects inheritance and polymorphism in [0:54] it [0:55] now comes the important question what [0:58] exactly is php [1:01] so we have spoken a lot about it so far [1:03] but do you know what it means [1:05] well php stands for hypertext [1:08] preprocessor [1:09] it is a server-side scripting language [1:11] that is embedded into html and used for [1:14] developing dynamic websites [1:17] now let's move ahead and understand the [1:19] concepts of oops in php [1:22] so as you can see on the screen i have [1:24] listed down the eight concepts of oops [1:27] that we will be discussing today [1:29] first is class [1:31] next comes objects then its member [1:33] function and member variables then [1:36] constructor and destructor [1:38] inheritance and finally it is [1:40] polymorphism [1:42] now let's walk through each of the [1:44] concepts one by one [1:46] first is class [1:48] it is a blueprint of an object that [1:50] provides initial values for the state a [1:54] class consists of both data and [1:56] functions and data and functions [1:57] together are called objects [2:00] now let's first have a look at the class [2:04] let me create a file [2:06] named class dot php [2:10] and let us write the code first [2:52] um [3:05] okay [4:04] here i have defined a class by using the [4:06] class keyword [4:07] followed by fruit which is the class [4:10] name [4:11] next i have added a pair of curly braces [4:14] and added the entire properties and [4:16] methods within the braces so as you can [4:19] see on the screen we have declared a [4:21] class named fruit [4:23] that contains two properties called name [4:26] and color [4:27] along with that i have also added two [4:29] methods set name and get name [4:34] now now the purpose of these methods is [4:36] for setting and getting the name [4:39] property [4:40] so let me give you some in important [4:42] information so when you define a class a [4:45] variable in it is called property while [4:48] a function is known as method [4:51] now let us execute the code and see the [4:53] output [4:57] let's go to the browser [4:59] and type localhost followed by the file [5:02] name [5:08] here here's a file class.php [5:11] and as you can see on the screen [5:13] the code is running perfectly [5:16] now that was about the class [5:19] next comes objects [5:21] so it is an instance of a class [5:23] basically [5:25] a variable holds a date of a class [5:27] now we define a class once [5:31] and create as many objects in it [5:34] for a better understanding let me give [5:36] you an example [5:37] suppose a car is a class so [5:40] mercedes-benz and bmw would be objects [5:43] now let's see objects [5:45] classes are incomplete without objects [5:48] so we can add as many objects as we want [5:51] from a class [5:52] and every single object has the [5:55] properties and methods defined in the [5:57] class but those objects will have [5:59] different property values [6:01] so objects of a class are created using [6:04] the new keyword [6:06] now let me open the code editor [6:10] and let's create another file [6:13] called [6:14] obj.php [6:18] so this code will be the same [6:21] let's copy [6:23] and paste it here [6:28] now let me type the code for object [6:33] [Music] [6:41] as you can see i have used the new [6:43] keyword to create object of the class [6:46] fruit [6:51] similarly [6:56] banana [6:57] would be another object [7:11] and now we'll use the set name function [7:16] to set [7:17] the name to apple [7:19] on this object on the apple object that [7:22] we have created [7:24] similarly [7:25] we will [7:27] set the name [7:31] for the banana object [7:44] there's an extra bracket here let's move [7:46] it [7:51] now moving forward let us type the code [7:56] to display [7:59] what we have added into the objects [8:04] for this can you guess which method will [8:07] be using [8:09] yes the get name method [8:12] will be used to display [8:15] the name saved onto the object using the [8:17] set name method [8:40] now that our code is complete [8:43] let us run and check [8:46] if it's working correctly [8:50] let me go back [8:52] and refresh [8:55] now here we have our object.php file [9:00] yes the code runs perfectly the fruit [9:02] program and it is displaying apple and [9:04] banana from the two objects that we [9:06] created i hope now you have clearly [9:09] understood the concept [9:11] moving on to the next one let's talk [9:13] about the member variable and member [9:15] function [9:16] a member variable is defined within a [9:18] class [9:20] the data can be accessed by the member [9:22] functions alone [9:23] now once the object is built these [9:25] variables are called attributes of the [9:27] object whereas member function is [9:30] defined within a class and is used to [9:32] access object data [9:34] if getting your learning started is half [9:36] the battle what if you could do that for [9:38] free [9:39] visit skill up by simply learn click on [9:41] the link in the description to know more [9:44] so next comes constructor [9:47] so it allows an individual to alert an [9:50] object's properties [9:52] while the object creation [9:54] while a destructor is a function that is [9:56] called when the object stops working [9: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