The community is working on translating this tutorial into Spanish, but it seems that no one has started the translation process for this article yet. If you can help us, then please click "More info".
Inheritance
Inheritance is one of the most important aspects of OOP. It allows a class to inherit members from another class. Understanding why this is smart without an example can be pretty difficult, so let's start with one of those.
Imagine that you need to represent various types of animals. You could create a Cat class, a Dog class and so on, but you would probably soon realize that these classes would share quite a bit of functionality. On the other hand, there could be stuff that would have to be specific for each animal. For a case like this, inheritance is really great. The idea is to create a base class, in this case called Animal, and then create a child class for each specific animal you need. Another advantage to this approach is that you will every animal you have will come with the same basic functionality that you can always rely on.
Again, this can seem very theoretic and you might not find it very useful in the beginning, but as you create more advanced websites, you will likely run into situations where inheritance can come in handy. Let's have a look at an example now:
class Animal
{
public $name;
public function Greet()
{
return "Hello, I'm some sort of animal and my name is " . $this->name;
}
}
A very simple class, pretty much like the ones we created in a previous chapter. However, "some sort of animal" is not very descriptive, so let's create a child class for a dog:
class Dog extends Animal
{
}
The dog is declared like a regular class, but after that, we use the extends keyword to tell PHP that the Dog class should inherit from the Animal class. Right now, our Dog class has the exact same functionality as the Animal class. Verify this by running the following code:
$dog = new Dog();
echo $dog->Greet();
You will see that both the name and the Greet() function is still there, but they are also still very anonymous. Let's change that by writing a specific version of the Greet() function for our Dog:
class Dog extends Animal
{
public function Greet()
{
return "Hello, I'm a dog and my name is " . $this->name;
}
}
Notice that we declare the Greet() function again, because we need for it to do something else, but the $name class variable is not declared - we already have that defined on the Animal class, which is just fine. As you can see, even though $name is not declared on the Dog class, we can still use it in its Greet() function. Now, with both classes declared, it's time to test them out. The following code will do that for us:
$animal = new Animal();
echo $animal->Greet();
$animal = new Dog();
$animal->name = "Bob";
echo $animal->Greet();
We start out by creating an instance of an Animal class and then call the Greet() function. The result should be the generic greeting we wrote first. After that, we assign a new instance of the Dog class to the $animal variable, assign a real name to our dog and then call the Greet() function again. This time, the Dog specific Greet() function is used and we get a more specific greeting from our animal, because it's now a dog.
Inheritance works recursively as well - you can create a class that inherits from the Dog class, which in turn inherits from the Animal class, for instance a Puppy class. The Puppy class will then have variables and methods from both the Dog and the Animal class.