Broad Network


Constant and Static Members in PHP Classes and Objects

Classes and Objects in PHP - Part 5

Foreword: In this part of the series, I talk about Constant and Static Members in PHP Classes and Objects.

By: Chrysanthus Date Published: 24 Nov 2018

Introduction

This is part 5 of my series, Classes and Objects in PHP. In this part of the series, I talk about Constant and Static Members in PHP Classes and Objects. You should have read the previous parts of the series before coming here, as this is the continuation.

Class Constant
It is possible to have a constant only for a class and its objects and not for the whole script. A constant variable or property is one whose value cannot change. You cannot access a constant property, without use of the class or object name.

The mathematics formula to determine the circumference of a circle is:

    Circumference = 2 X pii X radius

where pii is a constant and is equal to 3.14. This formula can be re-written as:

    Circumference = coefficient X pii X radius

where coefficient is 2. Abbreviating and letting Circumference be C and radius be r and coefficient be coef, the same formula can be re-written as:

    C = coef X pii X r

If r is 3, then the corresponding value for C would be given by,

        C = 2 X 3.14 X 3
=>    C = 18.84

Read and test the following script which has a class called, Circle with a method called, findCircumference(), to determine the circumference:

<?php

    class Circle
        {
            const pii = 3.14;

            public $r;
            public $coef;

            public function findCircumference()
                {
                    $circum = $this->coef * self::pii * $this->r;
                    return $circum;
                }
        }

    $obj = new Circle();
    $obj->r = 3;
    $obj->coef = 2;

    $C = $obj->findCircumference();
    echo $C;

?>

Note that pii is not preceded by $. To access pii within the class definition, you use the word, self, followed by the double colon operator, and not $this-> . Also note that X in programming is * . The output is:

    18.84

To access the constant from outside the class definition (description) use the class name followed by the scope resolution operator (which is the same as the double colon operator). Read and test the following code:

<?php

    class Circle
        {
            const pii = 3.14;

            public $r;
            public $coef;

        }

    echo Circle::pii;

?>

The output is:

    3.14

Class Constant and Visibility
Class constants may be defined as public, private, or protected. Constants declared without any explicit visibility reserved word, are public.

The self and parent Reserved Words
self is used in a class definition when you are referring to the constant or static variable (see below) of the current class. parent is used, when in the derived class definition, you are referring to the constant or static or constructor, of the base (parent) class.

From the constructor of the child class, you will refer to the constructor of the parent class as follows:

    function __construct()
        {
            parent::__construct();
            //initialize local variables here
        }

The mathematics formula to determine the arrea of a circle is:

    Area = pii X radius X radius

where pii is a constant and is equal to 3.14. Abbreviating and letting Area be A and radius be r, the same formula can be re-written as:

    A = pii X r X r

If r is 3, then the corresponding value for A would be given by,

        A = 3.14 X 3 X 3
=>    A = 28.26

Read and test the following code, where the derived class for Circle has a method, called findArea() to determine the area of a circle:

<?php

    class Circle
        {
            protected const pii = 3.14;

            private $r;
            public $coef;

            public function findCircumference()
                {
                    $circum = $this->coef * self::pii * $this->r;
                    return $circum;
                }
        }

    class ChildCircle extends Circle
        {

            public function findArea($ra)
                {
                    $Ar = parent::pii * $ra * $ra;
                    return $Ar;
                }
        }

    $childObj = new ChildCircle();
    $A = $childObj->findArea(3);
    echo $A;

?>

Note the use of "parent::";

The output is:

    28.26

Static Properties and Methods

Static Property
A static variable lives on after its surrounding code has been executed. Read and test the following code:

<?php

    class Cla
        {
            public static  $a = 0;

            public function mthd()
                {
                    self::$a = self::$a + 1;
                    echo self::$a, ' ';

                    if (self::$a < 20)
                        $this->mthd();
                }
        }

    $obj = new Cla();
    $obj->mthd();

    echo '<br>', Cla::$a;

?>

The output is:

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
    20

Note the way "self::" has been used to access the static variable within the class definition. You cannot use the arrow operator, -> to access the static variable with an object. If you want to access the static variable, at whatever value it is, outside the class definition, then begin with the class name followed by the double colon, and then the variable (including $).

Static Method
Declaring class properties or methods as static, makes them accessible without needing an instantiation (object) of the class. Read and test the following code:

<?php

    class Cla
        {
            private $a = 0;

            public static function mthd($a)
                {
                    $a = $a + 1;
                    echo $a, ' ';

                    if ($a < 20)
                        self::mthd($a);
                }
        }

    Cla::mthd(null);

?>

The output is:

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

Note that to access the ordinary member property within the static method definition, you do not use $this or self:: . However to call the static method itself, within its definition, you use self:: .

To call the static method from outside the class definition, you begin with the class name, followed by the double, and then the method call.

Note: A property declared as static cannot be accessed with an instantiated class object. However,  a static method can. Try the following code:

<?php

    class Cla
        {
            private $a = 0;

            public static function mthd($a)
                {
                    $a = $a + 1;
                    echo $a, ' ';

                    if ($a < 20)
                        self::mthd($a);
                }
        }

    $obj = new Cla();
    $obj->mthd(null);

?>

The output is:

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

Time to take a break. We stop here and continue in the next part of the series.

Chrys


Related Links

Basics of PHP with Security Considerations
White Space in PHP
PHP Data Types with Security Considerations
PHP Variables with Security Considerations
PHP Operators with Security Considerations
PHP Control Structures with Security Considerations
PHP String with Security Considerations
PHP Arrays with Security Considerations
PHP Functions with Security Considerations
PHP Return Statement
Exception Handling in PHP
Variable Scope in PHP
Constant in PHP
PHP Classes and Objects
Reference in PHP
PHP Regular Expressions with Security Considerations
Date and Time in PHP with Security Considerations
Files and Directories with Security Considerations in PHP
Writing a PHP Command Line Tool
PHP Core Number Basics and Testing
Validating Input in PHP
PHP Eval Function and Security Risks
PHP Multi-Dimensional Array with Security Consideration
Mathematics Functions for Everybody in PHP
PHP Cheat Sheet and Prevention Explained
More Related Links

Cousins

BACK NEXT

Comments