PHP Introduction | Structure and Object Orientation | Let's Create a Class
Now let us actually create and use a class. We will further modify the previous example that changed the display only for the text “PHP” inside a string, create a more general-purpose class, and use it.
Write the example code as follows.
<?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();
}
}
// Prepare instances
$title_obj = new TextModify('<span style="color:red;">', '</span>');
$title_obj->setFind("PHP");
$title_obj->setBody('Hello PHP!');
$msg_obj = new TextModify('<span style="color:blue;">', '</span>');
$msg_obj->setBody('Please write a sentence that includes the text PHP here.');
$rep_obj = new TextModify("<b>","</b>");
if ($_POST != null){
$str = $_POST['text1'];
$rep_obj->setBody($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 $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, a class named TextModify is created. Instances for the heading, message, and text replacement are prepared from it, and each function is called to create the display.
Looking at the TextModify class, several new things appear. Let us supplement the explanation.
1. Access Keywords
Here, four fields are provided: $header, $footer, $body, and $find. They store the replacement text header, meaning the text placed before the found text; the footer, meaning the text placed after it; the body, meaning the target text; and the search string.
Looking at these fields, private is attached at the beginning. This specifies how far the field can be used, and is called an “access keyword.” The following are available.
- private: Can be used only inside that class. It cannot be used from outside.
- protected: Can be used in that class and its subclasses.
- public: Can also be used outside the class.
Among these, “subclass” refers to the object-oriented feature called “inheritance.” This will be explained again later. For now, remember only that private means it can be used only inside the class, while public means it can be freely used from outside.
These fields store values that represent the properties of the object, so they should not be changed freely from the outside. Therefore, it is common to keep the fields themselves private, prepare separate methods for changing them, and manipulate the values by calling those methods from outside.
2. Constructor
Here, a public method named __construct is written. It has a special name and can be freely used from outside. This method is called a “constructor.” It is automatically called when an instance is created with new, and performs initialization for the instance. If it is not needed, it can be omitted.
This constructor prepares two arguments, so two arguments are also passed when calling new. Looking at the actual new call:
$rep_obj = new TextModify("<b>", "</b>");
The header and footer are specified as arguments in this way when creating the instance.
3. Access Methods
Among the methods provided by the class, several manipulate fields: setHeader, setFooter, setBody, and setFind.
As mentioned earlier, it is common to make fields private and prepare separate methods to get or change their values. These methods are called “accessors.”
Accessors are generally declared with names like getFieldName and setFieldName. This is not absolutely required, but naming them according to this rule makes them easier to understand.
4. About $this
Methods often perform work on “the object itself.” Classes are generally used by creating instances, but what should you do when you want to use fields or methods inside the current instance itself?
In such cases, the variable $this is used. It represents “this instance itself.” By using it, you can call the instance’s own features.
With these points in mind, look at the code again. You should be able to understand how the class and instances work.