PHP Introduction | Structure and Object Orientation | Class Methods and Class Fields

So far, we have explained that the basic idea is to create and manipulate instances from a class. However, this is not always required. You can also use a class as a class. To do that, however, methods and fields must be written in a different form.

Methods and fields that can be used directly from the class rather than from an instance are called “class methods” and “class fields.” If you think of a class simply as a blueprint, it may feel strange that values can be stored or methods can be executed inside the class itself. But it is helpful to imagine that a class is also an object, and instances are objects created based on the class object.

Methods and fields provided by a class are prefixed with the keyword static. This means “static,” so class methods and class fields are also called “static methods” and “static fields.”

Let us briefly summarize the basic usage. Definitions are written as follows.

static $variable;
static function method(parameter) {...}

You only need to place static at the beginning. When using an access keyword, write it after static, as in static private $hoge;. These fields and methods are called as follows.

Class::$variable;
Class::method(argument);

Write the class name followed by the :: symbol. Be careful with fields. Instance fields are written like class->variable, but class fields are written like Class::$variable, including the $.

Let us look at an actual example.

<?php
class TextModify {
    static private $header = "<b>";
    static private $footer = "</b>";
    static private $find = "PHP";
     
    static function setTagData($h,$f) {
        self::$header = $h;
        self::$footer = $f;
    }
     
    static function setFind($f) {
        self::$find = $f;
    }
     
    static function writeRenderText($s) {
        $res = str_replace(self::$find, self::$header . self::$find . self::$footer, $s);
        echo $res;
    }
}
     
// Prepare the class
TextModify::setTagData('<span style="font-size:200%;">', '</span>');
TextModify::setFInd('PHP');
 
if ($_POST != null){
    $str = $_POST['text1'];
}
?>
<!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
        TextModify::writeRenderText('Hello PHP!');
        ?>
        </h1>
        <p><?php TextModify::writeRenderText('Please write a sentence that includes the text PHP here.'); ?></p>
        <hr>
        <p><?php TextModify::writeRenderText($str); ?></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>

This example rewrites the earlier TextModify class so that it uses class methods and class fields. Here, the text PHP is displayed at twice the font size.

In this example, setTagData sets the tags to place before and after the text, and setFind sets the search string.

TextModify::setTagData('<span style="font-size:200%;">', '</span>');
TextModify::setFInd('PHP');

No instance is created with new; values are set directly on the class. When outputting text, the method is simply called like this.

TextModify::writeRenderText('Hello PHP!');

It is used very simply.

Using class methods and class fields lets you call features very easily like this without creating an instance. On the other hand, when an object holds various data and behaves according to that data, you must set many values again each time you use it, which can become complex.

When holding many data values and processing based on them, creating and using instances is much more convenient. Class methods are good when there is only data that almost never changes, or when you simply call fixed features.

For example, when grouping numerical calculations into a class, such as passing numbers as arguments and receiving a result, it is more convenient to call them easily as class methods than to create an instance every time. Use each approach according to its purpose.