Double colon:: Usage in PHP _php instance

Source: Internet
Author: User
Tags access properties class definition getcolor

A few days ago in Baidu know that someone asked in PHP double colon:: The use of the answer to him at that time is more concise because the phone typing is not convenient! Today suddenly remembered, so here is a summary of the double colon I encountered:: In PHP use of the situation!

The double colon operator, the scope-qualified operator scope resolution operator, can access properties and methods that are overridden in static, const, and classes.
Used outside the class definition, the class name is invoked. In PHP 5.3.0, you can use variables instead of class names.

Program List: Defining external access with variables in a class

<?php
class Fruit {
 Const Const_value = ' Fruit Color ';
}
$classname = ' Fruit ';
echo $classname:: Const_value; As of PHP.
echo fruit::const_value;
? > Program
List: Used 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: Invoking the Parent method

<?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 scoped Qualifiers

<?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 to invoke the base class

<?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

The above content for everyone to explain:: In the use of PHP, I hope you like.

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.