Range resolution operator (::)

Source: Internet
Author: User
Tags class definition zend

The range resolution operator (also known as Paamayim Nekudotayim) or, more simply, a pair of colons, can be used to access static members, class constants, and also to override properties and methods in a class.

Use the class name when referencing these items outside of the class definition.

From PHP 5.3.0, you can refer to a class by using a variable that cannot be a keyword (such as self,parent and Static).

It seems strange to have Paamayim Nekudotayim chosen as the name of the double colon operator. However, this is the decision the Zend development team made when writing Zend Engine 0.5 (used in PHP 3). In fact, the word means double colon in Hebrew.

Example #1 used outside of a class:: operator

<?php
class MyClass {
    Const Const_value = ' A constant VALUE ';
}

$classname = ' MyClass ';
echo $classname:: Const_value; From PHP 5.3.0

, echo myclass::const_value;
? >
Self, parent, and static these three special keywords are used to access the properties or methods within the class definition.
Example #2 used within the class definition::

<?php
class Otherclass extends MyClass
{public
    static $my _static = ' static var ';

    public static function Doublecolon () {
        echo parent::const_value. "\ n";
        echo Self:: $my _static. "\ n";
    }
}

$classname = ' Otherclass ';
echo $classname::d Oublecolon (); From PHP 5.3.0

Otherclass::d Oublecolon ();
? >

Example #3 The method of calling the parent class


<?php
class MyClass
{
    protected function MyFunc () {
        echo "myclass::myfunc () \ n";
    }
}

class Otherclass extends MyClass
{//
    overrides the definition public
    function MyFunc () of the parent class ()
    {
        //But still can invoke the overridden method in the parent class
        Parent::myfunc ();
        echo "Otherclass::myfunc () \ n";
    }
}

$class = new Otherclass ();
$class->myfunc ();
? >


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.