So next up we are going to learn about the inheritance and polymorphism.
So we've got a person class already.
Now let's create another class that would be based on this person class.
This is called inheritance.
I am defining a class that will be called employee.
I use the keyword extends to tell PHP that this class is based on person.
So that's inheritance because it inherits all the fields and all the methods of the parent class, but
it can add something on its own.
Now let me copy the constructor from the person to the employee.
And we are doing this because I'd like to add another fields in here.
So this would be a string called position.
So employee has position at work.
Okay, now I'd like the message for the employee to be a little different than the one from person.
Additionally, when introducing an employee, I'd like to know what's the position of an employee?
Now I can create an employee right now by using new employee.
I can pass the name.
I can pass the age and I can pass the position.
Let's say that that's a manager.
Now, when I echo the employee introduce method.
This should all work fine.
So I'm Jerry and I'm 45 years old.
This is matching the data that we've passed to the employee class.
So the things that we should learn in here is that the employee has all the data and all the methods
of the base class.
So person is the base class.
This method is only defined in the person class.
Yet when we create an employee which extends person, we can call this method.
Now in our case, using this introduce method from the base class is not ideal because the base class
does not know about the position, it's only about the person.
So when introducing an employee, it's still only going to mention the name and the age.
So what we can do is we can overwrite this method.
That's important.
Word overriding.
Overriding means we redefine the method that's specific for an employee.
And now I can say I work as a this position.
Dot.
Now.
Let's see how the message will look like right now.
By the way, let me concatenate that with a new line.
Okay, let's run this.
I work as a manager.
Mm.
So we've got the information that we wanted to convey.
We state the position, but we are losing the previous behavior.
So it might look like we are actually doing something that doesn't really benefit us.
And to fix this.
We just need to make sure we are also using the base class method and to access inside the classes the
methods or data from the parent class.
We use the special parent syntax double colon, and that way I can call the introduce method from the
parent, which should just output the text.
That introduces the person.
And I can add a space here so we can concatenate that with our own more specific implementation.
So now when I run this we've got the base introduction plus the position.
Okay.
So now let's see an example of polymorphism.
Instead of just one variable.
Let's define an array of people.
Now the first element in this array would be our employee Jerry.
But we're also going to have a person, just a person that doesn't work anywhere.
That would be me.
So we can still use this person class.
Now let me define a function that will be called introduce and it accepts an argument of type person.
Now, thanks to polymorphism, when we have a common parent class, we can treat objects of different
classes as if they are the same parent class.
So in case of this simple method, I can do echo person introduce and I concatenate that with a new
line.
Now let's also change the name of this variable to people not employees because it contains different
classes, but they all have the same base class.
Now, in the script I'm just going to do for each.
People as person.
And I will just call the introduce function, which accepts an object of class person.
And with polymorphism, actually both the employee and person can be treated as if they are the person
class, because the object which is of class person is actually of this specific class, and the employee
extends the person, which means it will have the introduce method.
For example.
So I can call introduce passing the person.
Now let's run this.
And those objects have their specific implementation of the introduce method with Jerry.
This will add his position.
Now with me it's only saying that I'm pure and I'm 37 Seven.
Now, at this point, I'd like you to appreciate how important polymorphism is.
Now, what you see here is we've created a function that accepts an argument, and you've just seen
us passing objects of different classes, yet everything has worked perfectly.
And the same method could be run for different objects, yet it yielded different results.
So that is the power of polymorphism that you can create code that doesn't have to change if some specific
implementation on your classes changes.
So we just have a base class which has some base methods and we can depend on that.
And if we then decide to add more classes like a boss class or a director class, this code does not
have to change.
It will still function and it will still properly introduce everyone.