Application of double colon in PHP

Source: Internet
Author: User
Tags access properties getcolor php class
The PHP class code often sees the "::" operator, which is scoped to the scope operator, and is represented by a double-colon "::", which is used to pin the levels of different scopes in the class. The left side is the member of the access scope to the right of the scope.

The scopes defined in PHP are self and parent two (a static scope is provided in PHP6).

Self: Represents the scope of the current class, unlike this, which does not represent a particular instance of a class, does not work in code other than the class, and it does not recognize its position in the hierarchy of inheritance. That is, when you use self in an extension class, it calls a method other than the parent class, but instead extends the overloaded method of the class.

Parent: Represents the scope of the current class parent class, and the rest is the same as the self attribute.

Example PHP double colon:: operator:

<?phpclass forasp{  static $url = "Http://blog.csdn.net/abandonship";  Static $webname = "The use of double colons for PHP learning";  Public Function Writeurl () {    echo self:: $url;//Call own content  } public  function Writewebname () {    echo " The test subclass invokes the parent class content ";  }} Class CN extends forasp{  function father () {    parent::wirtewebname ();}  } $a = new forasp ();//Instantiate parent class $a->writeurl ();//Call own content $b = new cn (); $b->writewebname ();//Call Parent content?>

You can also use:: To invoke static methods or properties in a class in a call to a static method, which reduces resource usage because instances of each class occupy a portion of the resource.

Php6 in the static:: Scope, is we no longer need self:: and Parent::. When you want a class that points to the final implementation functionality, use static: This qualifier calculates the members of the last class in the inheritance layer immediately before the code executes, a process called deferred binding.

The double-colon operator, or scope Resolution Operator, can access properties and methods overridden in static, const, and class classes, or also known as scope-qualified operators.
Used outside of the class definition, called with the class name. In PHP 5.3.0, you can use variables instead of class names.

Program List: Accessing the class definition externally with a variable

<?phpclass Fruit {  const Const_value = ' Fruit Color ';} $classname = ' Fruit '; Echo $classname:: Const_value; As of PHP 5.3.0echo fruit::const_value;? >

Program List: Use double colons outside the class definition (::)

<?php  class Fruit {    Const Const_value = ' Fruit Color ';  }  Class Apple extends Fruit  {public    static $color = ' Red ';    public static function Doublecolon () {      echo parent::const_value. "\ n";      echo Self:: $color. "\ n";    }  }  Apple::d Oublecolon ();  ? >


Program Run Result:

Fruit Color Red

Program List: Calling the parent method


PHP code

<?php  class Fruit  {      protected function Showcolor () {          echo "fruit::showcolor () \ n";      }  }    Class Apple extends Fruit  {      //Override parent ' s definition public      function Showcolor ()      } {          //But Still call the parent function          Parent::showcolor ();          echo "Apple::showcolor () \ n";      }  }    $apple = new Apple ();  $apple->showcolor ();  ? >


Program Run Result:
Fruit::showcolor ()
Apple::showcolor ()

Program List: Using scope qualifiers


PHP code

<?php      class Apple      {public          function Showcolor ()          {              return $this->color;          }      }      Class Banana      {public          $color;          Public function __construct ()          {              $this->color = ' Banana is yellow ';          }          Public Function GetColor ()          {              return apple::showcolor ();          }      }      $banana = new Banana;      echo $banana->getcolor ();  ? >


Program Run Result:
Banana is yellow

Program List: Methods for calling base classes

PHP code

<?php    class Fruit  {      static function color ()      {          return ' color ';      }        static function Showcolor ()      {          echo ' show '. Self::color ();      }  }    Class Apple extends Fruit  {      static function color ()      {          return ' red ';      }  }    Apple::showcolor ();  Output is "Show color"!    ? >


Program Run Result:
Show Color

  • 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.