PHP Introduction | Structure and Object Orientation | Extending Features Through Inheritance

In object orientation, classes you create can be reused in many places. However, when you actually try to use them, you may want to modify part of them or extend them because they are a little insufficient as features.

In such cases, rewriting and changing the class code is one method. But if the class is already used in many places in a large project, you should not freely change its contents. On the other hand, copying the class code and creating another new class is wasteful. Creating several classes that are almost the same and differ only slightly wastes code.

The idea created to reuse classes without touching the original class and with only minimal modification is called “inheritance.”

Inheritance creates a new class while taking over all the features of an existing class as they are. It can be used by defining a class as follows.

class ClassName extends ClassToInherit {...}

The source class being inherited from is called the “superclass,” and the newly created class that inherits from it is called the “subclass.” A subclass inherits all fields and methods of the superclass and can use them as they are. However, members with the access keyword private are hidden and cannot be used.

The following is a further modification of the previous example.

<?php
class TextModify {
    private $header = "<b>";
    private $footer = "</b>";
    private $body = "";
    private $find = "PHP";
     
    public function __construct($h,$f){
        $this->setHeader($h);
        $this->setFooter($f);
    }
     
    function setHeader($s){
        $this->header = $s;
    }
     
    function setFooter($s){
        $this->footer = $s;
    }
     
    function setBody($s){
        $this->body = htmlspecialchars(strtoupper($s));
    }
    function setFind($s){
        $this->find = $s;
    }
     
    function getRenderText(){
            $res = str_replace($this->find, $this->header . $this->find . $this->footer, $this->body);
        return $res;
    }
     
    function writeRenderText(){
        echo $this->getRenderText();
    }
}
 
class TitleModify extends TextModify {
     
    public function __construct($s){
        $this->setHeader('<span style="color:red;">');
        $this->setFooter('</span>');
        $this->setFind('PHP');
        $this->setBody($s);
    }
}
 
class MsgModify extends TextModify {
     
    public function __construct($s){
        $this->setHeader('<span style="color:blue;">');
        $this->setFooter('</span>');
        $this->setFind('PHP');
        $this->setBody($s);
    }
}
 
class RepModify extends TextModify {
     
    public function __construct($s){
        $this->setHeader('<b>');
        $this->setFooter('</b>');
        $this->setFind('PHP');
        $this->setBody($s);
    }
}
 
// Prepare instances
$title_obj = new TitleModify('Hello PHP!');
$msg_obj = new MsgModify('Please write a sentence that includes the text PHP here.');
if ($_POST != null){
    $str = $_POST['text1'];
    $rep_obj = new RepModify($str);
}
?>
<!DOCTYPE html>
<html lang="ko">
    <head> 
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
        <title>sample page</title>
    </head>
    <body>
        <h1><?php $title_obj->writeRenderText(); ?></h1>
        <p><?php $msg_obj->writeRenderText(); ?></p>
        <hr>
        <p><?php if (isset($rep_obj)) $rep_obj->writeRenderText(); ?></p>
        <form method="post" action="./index.php">
            <textarea name="text1" cols="40" rows="5"><?php echo $str; ?></textarea>
            <br><input type="submit">
        </form>
        <hr>
    </body>
</html>

Here, the TextModify class is inherited to create subclasses named TitleModify, MsgModify, and RepModify. Each class prepares its own __construct so that the necessary settings can be made.

These subclasses have only constructors, but inside those constructors, the values of each field are set through access methods. The actual output uses writeRenderText. You can see that the features of the superclass TextModify can be used as they are.

By inheriting classes and reusing them while extending their features, you can create classes more flexibly.