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.