Broad Network


The Double Colon Operator and the final Reserved Word in PHP

Object Oriented Programming in PHP – Part 4

Forward: In this part of the series, we see how and when to use the double colon operator, so far as classes are concerned in PHP.

By: Chrysanthus Date Published: 9 Aug 2012

Introduction

This is part 4 of my series, Object Oriented Programming in PHP. In this part of the series, we see how and when to use the double colon operator, so far as classes are concerned in PHP. In this article, I call class definition, class description; this refers to the actual code of your class.

Do not confuse between the class and an instantiated object. Consider the class as the description (code) of the class. An instantiated object is a closed entity, like electronic equipment and you generally work with its outer characteristics. An instantiated object is an object created from the class and kept in a different place in memory.

Many objects can be instantiated from one class. Many objects can also be instantiated from one derived (inherited) class. Any feature that is in a class is also in its corresponding instantiated objects, whether it is a base or derived class. In this tutorial we see how to access a method of a class (not the object) from outside the class and how to access certain special features from within the class. You need mainly the double colon operator for all that.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

The Double Colon Operator
The double colon operator is just, :: .

Accessing Class Methods from Outside the Class
Read and try the following program which shows how to access methods of a class (description) from outside the class:

<?php

    class Cla
        {
            public $prop;
   
            public function mthd()
                {
                    return "I have been seen.";
                }
        }

    $rVar = Cla::mthd();

    echo $rVar;

?>

The self and parent Reserved Words
A class can have a constant property and/or a static (see later) property. A derived class can override the inherited methods of its parent class. In a class description, if you want to access a constant property or static property, use the reserved word, self, followed by the double colon and then the name of the property without any preceding $. In a derived class, if you want to override (modify) an inherited method, use the reserved word, parent, followed by the double colon and then the name of the method.

Accessing constant Property within the Class
Use the reserved word, self, to access a constant or static property within the class description, like in the following example:

<?php

    class Cla
        {
            const pi = 3.14;
    
            public function display()
                {
                    $var = self::pi * 10;
                    echo $var;
                }
        }

    $obj = new Cla();

    $obj->display();

?>

Modifying inherited Members
Use the reserved word, parent, to override (modify) a method within the class description, like in the following example:

<?php

    class Cla
        {

            public function display()
                {
                    echo "I am of the parent.<br />";
                }
        }

    class derivedCla extends Cla
        {
            public function display()
                {
                    parent::display();
                    echo "I am of the child.<br />";
                }
        }


    $derObj = new derivedCla();

    $derObj->display();

?>

To override a parent method in a derived class description, retype the parent method, but change the content of the block of the method. If you want to add statements to the overridden method (maintaining the previous statements), that is when you use the scope operator to fit all the statements of the parent method in the overridden method, like in the above example.

The final Reserved Word
PHP has a reserved word called, final. It is used with the definition of a method or the definition of a class. In other words, you can make a method final (see below) or a class final (see below).

Final Method
If a method is made final, it means a derived class cannot override the method. The following code shows this:

<?php

    class Cla
        {

            final public function display()
                {
                    echo "I am of the parent.<br />";
                }
        }

    class derivedCla extends Cla
        {
            //You cannot override the public method, display(), in this derived class.
        }

?>

The word, final, precedes the definition of the method.

Final Class
If a class is made final, it means the class cannot be inherited; that is you cannot make a child class from it. The following code shows this:

<?php

    final class Cla
        {

            public $prop;

            public function display()
                {
                    echo "I am of the parent.<br />";
                }
        }

    //You cannot extend the class, Cla.

?>

The word, final, precedes the description (definition) of the class.

Chrys

Related Links

Major in Website Design
Web Development Course
HTML Course
CSS Course
ECMAScript Course
PHP Course
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message