Scope 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, a variable can be used to refer to a class, 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

<?php
class  MyClass  {
    const  CONST_VALUE  =  ‘A constant value‘ ;
}

$classname  =  ‘MyClass‘ ;
echo  $classname :: CONST_VALUE ;  // 自 PHP 5.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::

<?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 :: doubleColon ();  // 自 PHP 5.3.0 起

OtherClass :: doubleColon ();
?>

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

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

class  OtherClass  extends  MyClass
{
     // 覆盖了父类的定义
     public function  myFunc ()
    {
         // 但还是可以调用父类中被覆盖的方法
         parent :: myFunc ();
        echo  "OtherClass::myFunc()\n" ;
    }
}

$class  = new  OtherClass ();
$class -> myFunc ();
?>

Scope resolution operator (::)

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.