‫Now we're going to look at abstract classes, which are kind of hard to explain, but I think that once
‫you see how they work, it'll become more clear.
‫So basically abstract classes, they can't be instantiated like a regular class, but they can be subclass.
‫So you can create a subclass that inherits an abstract class.
‫So let's let's go ahead and give you an example here.
‫So we'll create a class an abstract class called shape.
‫So to create an abstract class we need to prefix it with the word abstract.
‫So we'll say class shape.
‫And you can probably see where I'm where I'm going to go with this.
‫This shape abstract class can be inherited by certain shapes like circles squares etcetera.
‫And we can have properties.
‫So we're going to have a protected property called name.
‫All right.
‫And remember protected means that it can it can only be accessed from within the class or any class
‫that inherits it.
‫Any subclass.
‫Now we can also have abstract methods within an abstract class.
‫And we're not going to add the functionality here in the method in the abstract class.
‫We'll do that in the class that extends it or inherits from it.
‫So we can say abstract and then public.
‫Function.
‫And let's call this calculate area.
‫And that's it.
‫We're going to put a semicolon here.
‫We're not going to do anything else with this abstract function or method.
‫All right.
‫Now we can have regular methods in here as well which are called concrete methods.
‫We can also have a constructor.
‫So let's say public function underscore construct.
‫And let's say we pass in a name, and then we set that name or set the name property.
‫So this name to the name that's passed in.
‫And then I'm going to create what's called a concrete.
‫Let's say concrete.
‫Method, and this here is an abstract method.
‫And the concrete method is just going to be to get the name.
‫So let's say public function get name.
‫And that's just going to return this.
‫This name.
‫All right.
‫So that's our abstract class that has one abstract method.
‫Now I should mention that if you have an app one, one or more abstract methods, then your class has
‫to be abstract, right?
‫So if I run this nothing happens obviously.
‫But if I were to get rid of the abstract on the class but still have a method, you'll see I'm already
‫getting an error if I reload it says class shape contains one abstract method, therefore must be declared
‫abstract.
‫Okay, so you can't just have an abstract method inside of a regular class.
‫So now let's create a subclass of shape called circle.
‫So class circle.
‫And we want to extend the shape class.
‫So we extend it just like we would with any other class.
‫All right.
‫So in here actually what's this say does not implement method calculate area.
‫So this is this is actually something I should mention.
‫If you extend a class that has an abstract method you have to implement that method.
‫And you can see I'm not implementing calculate area here.
‫But I'll get to that.
‫So I do want to have a property a private property.
‫Oops.
‫Private property of radius.
‫Because this is a circle and we're going to have a constructor.
‫So public function.
‫Construct.
‫And that's going to take in name.
‫And it's also going to take in radius.
‫And then we're going to we want to inherit the parent constructor.
‫So we can do that with parent.
‫Double colon and then underscore construct.
‫Okay.
‫I showed you this in the inheritance video.
‫And we want to pass in name.
‫So that will handle the name.
‫And then let's set this.
‫This radius to the radius that's passed in.
‫We don't have to do this name equals name because that's done up here in the parent constructor.
‫And that's being run with this line here.
‫So now we need to implement the the calculate area.
‫So let's come down here and say public function.
‫Calculate area.
‫And for that I'm going to return and we're going to use the pi function.
‫And then multiplied by the pow function.
‫So power of.
‫And let's pass in the radius.
‫So this radius and two not one two.
‫So that should get us the area.
‫Now to use this we're going to come out outside of the class.
‫And we want to instantiate a circle class.
‫Remember the shape the abstract class can't be instantiated.
‫So let's say circle.
‫And we'll set that to a new circle object using the circle class.
‫The name I'm going to pass in as circle, and then the radius I'll pass in as five.
‫Okay.
‫And then we can just do a var dump.
‫Of circle.
‫And we should see the circle object or not.
‫Class circle not found.
‫Um.
‫Oh, I spelled it wrong.
‫Circle.
‫Okay, so let's reload that.
‫And you can see we have a circle object.
‫We have the name of circle.
‫So right here is the name.
‫And then we have the the radius of five.
‫And you could do do what you want with that.
‫Now usually when you have an abstract class you have multiple classes that extend it.
‫So let's create another class here.
‫Called rectangle.
‫And that's going to extends shape.
‫Now a rectangle obviously is going to have different properties.
‫So we'd say we'll say private width.
‫And let's say, private height.
‫And then let's create our constructor.
‫So it's going to be similar to this.
‫So I'll just grab that and it's going to take in a name.
‫But instead of a radius it's going to take in the height and the width.
‫Okay.
‫And then we just want to set that here.
‫So we'll set that to height.
‫This height.
‫And with.
‫Set that to the width that's passed in.
‫Okay.
‫And then we'll have the calculate area here as well.
‫And you can see we have to implement it or we get an error.
‫So I'll just grab that.
‫Except to calculate the area of a rectangle is going to be different.
‫And this is a good example of polymorphism.
‫We have to calculate areas, but they do different things.
‫They work in different ways.
‫So for the area of the rectangle we're going to say this width multiplied by this.
‫Height.
‫Okay.
‫And let's just come down here and let's, let's get rid of that and we'll say rectangle.
‫Equals new rectangle class.
‫And it's going to take in a name which I'll just call rectangle.
‫And the, let's say, the height and width, I'll pass in as four and six.
‫Okay.
‫And then let's just do an echo.
‫So we'll say echo.
‫The.
‫Let's do the circle first.
‫So circle.
‫The hell was that circle?
‫And I want to get the name.
‫So we'll use Get name.
‫Now get Name is on the abstract class, but it's a concrete method so I can use it.
‫Okay.
‫Even though I didn't instantiate a shape class, these are inheriting from it, so I can use get name
‫or any concrete method here.
‫So we'll use Getname.
‫And then let's concatenate on space and we'll say area colon space.
‫And then let's concatenate onto that circle.
‫And then we want to calculate area.
‫Okay.
‫And then I'll just add a line break onto that.
‫All right.
‫So let's try that out.
‫And that's what we get for the area.
‫We pass in the five radius.
‫That's what we get.
‫So if I pass in let's say 15 radius reload.
‫That's what we get.
‫And we can do the same with the rectangle.
‫So let's change.
‫Let's see.
‫We'll change this.
‫Two rectangle.
‫So get the name and then the area.
‫Let's reload.
‫And for rectangle area we get 24.
‫If I change this to let's say seven and ten and reload then we get 70.
‫All right.
‫So that's that's how you can use an abstract class.
‫It's kind of like a, like a map or a signature of of the classes that you're inheriting from it.
‫So next we're going to look at interfaces.