Inheritance in PHP

We will talk about this peculiar characteristic to make independent and customized copies of already built classes, typical of object-oriented programming.

Object-oriented programming has a mechanism called inheritance by which classes can be defined from other classes. Classes made from another class, or rather, that extend another class, are called extended classes or derived classes.

Extended classes inherit all the attributes and methods of the base class. Also, they can have as many new attributes and methods as desired.

To extend the example that we have been developing, the Box class, we are going to create an extended class called Box_thematic. This class inherits from box, but it also has a “theme”, which is the description of the type of things that we put in the box. With this we can have several boxes, each one with things of a specific theme.

class Thematic_Box extends Box{ var $theme; function define_theme($new_theme){ $this->theme = $new_theme; } }

In this class we inherit from Box, with which we have at our disposal all the attributes and methods of the base class. In addition, a new attribute has been defined, called $theme, and a method, called define_theme(), that receives the theme with which you want to label the box.

We could use the Caja_tematica class in a similar way to how we used the original Caja class.

$mica_thematic_box = new Thematic_box(); $micaja_tematica->define_tema(“Cables and connectors”); $micaja_tematica->introduce(“Network cable”); $micaja_tematica->introduce(“RJ45 connector”); $micaja_thematic->sample_content();

In this case, the result you get is similar to what you get for the base class. However, when the content of a box is shown, the most interesting thing would be to also indicate the type of objects that the thematic box contains. To do this, we have to redefine the show_content() method.

See also  /faq/496.php

Override methods in extended classes

Redefining methods means recoding them, that is, rewriting your code for the extended class. In this case, we have to redefine the show_content() method so that it also shows the theme of the box.

To redefine a method, all we have to do is rewrite it inside the extended class.

function display_content(){ echo “Content of box” . $this->theme . “: ” . $this->content; }

In this example we have recoded the integer method to display the entire data.

Sometimes it is very useful to rely on the definition of a method of the base class to carry out the actions of the extended class. For example, for this example, we have to define a constructor for the Box_thematic class, in which the theme of the box is also initialized. Since there is already a constructor method in the base class, it is not worth rewriting its code, it is best to call the constructor that was defined in the original Box class, which will initialize all the data in the base class, and then perform initialization for the attributes of the extended class itself.

To call a method of the parent class within the code of a method that we are overriding, we use a syntax like this:

function Theme_Box($height=1,$width=1,$length=1,$color=”black”,$theme=”Unsorted”){ parent::Box($height,$width,$length,$color ); $this->theme=$theme; }

Here we see the redefinition of the constructor, of the Caja class, for the Caja_tematica class. The constructor first makes a call to the constructor of the base class, via a reference to “parent”. Then it initializes the value of the $theme attribute, which is specific to the theme_box.

See also  Service Workers

In the same line of work, we can redefine the sample_content() method based on the one that was declared in the base class. The code would be as follows:

function display_content(){ echo “Content of box” . $this->theme . “: “; parent::display_content(); }

Loading Facebook Comments ...
Loading Disqus Comments ...