PHP Range resolution operator (::)

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

Use the class name when referring to these items outside of the class definition.

From PHP 5.3.0, the class can be referenced by a variable, and the value of the variable cannot be a keyword (such as self,parent and Static).

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

Example #1 used outside the class:: operator

Class myclass{    Const CONST_VALUE = 1;} $classname = ' MyClass '; Echo $classname:: Const_value; From PHP5.3.0 Echo myclass::const_value;

Self,parent and static These three special keywords are used to access their properties or methods within the class definition.

Example #2 used inside the class definition::

function __autoload ($classname) {    require_once ($classname. "). PHP "); Class Otherclass extends myclass{public    static $my _static = 1;    public static function Doublecolon () {        echo parent::const_value. ' <br> ';        echo Self:: $my _static, ' <br> ';    }} $classname = ' Otherclass '; Echo $classname::d Oublecolon (); Otherclass::d Oublecolon ();

When a subclass overrides a method in its parent class, PHP does not invoke methods that have been overridden in the parent class. Whether the method of calling the parent class depends on the child class. This mechanism is also used for constructors and destructors, overloading, and magic methods.

Example #3 Calling a method of the parent class

Class myclass{    protected function MyFunc ()    {        echo ' myclass::myfunc () <br> ';    }} Class Otherclass extends myclass{//    overrides the definition of the parent class public    function MyFunc ()    {        //But can still call the overridden method in the parent class        Parent::myfunc ();        Echo ' Otherclass::myfunc () <br> ';    }} $class = new Otherclass (), $class-MyFunc ();

Output Result:

Myclass::myfunc ()

Otherclass::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.