Application of Double colons in php

Source: Internet
Author: User

In php code, we often see the ":" operator, which is a limited scope operator. It is represented by a double colon ":", which is used to pin the levels of different scopes in the class. On the left is the access scope member on the right of the scope.

The scope defined in php is self and parent (static scope is provided in php6 ).

Self: indicates the scope of the current class. Unlike this, self does not represent a specific instance of the class and cannot be used in code outside the class, in addition, it cannot identify its position in the hierarchy of inheritance. That is to say, when self is used in an extension class, it calls not the method of the parent class, but the method of overloading the extension class.


Parent: indicates the scope of the current class parent class, and the rest are the same as the self feature.


For example, php double Colon: Operator:

View plaincopy to clipboardprint?
<? Php
Class forasp {
Static $ url = "http://blog.csdn.net/abandonship ";
Static $ webname = "usage of double colons for PHP learning ";
Public function writeurl (){
Echo self: $ url; // call your own content
}
Public function writewebname (){
Echo "test subclass calling parent class content ";
}
}

Class cn extends forasp {
Function father (){
Parent: wirtewebname ();
}
}

$ A = new forasp (); // instantiate the parent class
$ A-> writeurl (); // call its own content
$ B = new cn ();
$ B-> writewebname (); // call the content of the parent class
?>
<? Php
Class forasp {
Static $ url = "http://blog.csdn.net/abandonship ";
Static $ webname = "usage of double colons for PHP learning ";
Public function writeurl (){
Echo self: $ url; // call your own content
}
Public function writewebname (){
Echo "test subclass calling parent class content ";
}
}

Class cn extends forasp {
Function father (){
Parent: wirtewebname ();
}
}

$ A = new forasp (); // instantiate the parent class
$ A-> writeurl (); // call its own content
$ B = new cn ();
$ B-> writewebname (); // call the content of the parent class
?>


You can also use: to call static methods or attributes in a class to reduce resource usage, because each class instance occupies part of the resource.


In php6, static: scope is proposed. We no longer need self: and parent ::. When you want to point to the class that implements the final function, use static:. This qualifier will calculate the last class member in the inheritance layer immediately before the code is executed, this process is called delayed binding.

The "Double colon Operator", or "Scope Resolution Operator", can access static, const, and attributes and methods that are rewritten in a class.
Use the class name for calling when it is used outside the class definition. In PHP 5.3.0, you can use variables instead of class names.


Program List: Use variables to define external access in the class
View plaincopy to clipboardprint?
<? Php
Class Fruit {
Const CONST_VALUE = Fruit Color;
}
$ Classname = Fruit;
Echo $ classname: CONST_VALUE; // As of PHP 5.3.0
Echo Fruit: CONST_VALUE;
?>
<? Php
Class Fruit {
Const CONST_VALUE = Fruit Color;
}
$ Classname = Fruit;
Echo $ classname: CONST_VALUE; // As of PHP 5.3.0
Echo Fruit: CONST_VALUE;
?>

 

Program List: Use double colons (: :) outside the class definition (::)
View plaincopy to clipboardprint?
<? Php
Class Fruit {
Const CONST_VALUE = Fruit Color;
}
Class Apple extends Fruit
{
Public static $ color = Red;
Public static function doubleColon (){
Echo parent: CONST_VALUE ."";
Echo self: $ color ."";
}
}
Apple: doubleColon ();
?>
<? Php
Class Fruit {
Const CONST_VALUE = Fruit Color;
}
Class Apple extends Fruit
{
Public static $ color = Red;
Public static function doubleColon (){
Echo parent: CONST_VALUE ."";
Echo self: $ color ."";
}
}
Apple: doubleColon ();
?>


 

Program running result:

Fruit Color Red


Program List: Call the parent Method

View plaincopy to clipboardprint?
<? Php
Class Fruit
{
Protected function showColor (){
Echo "Fruit: showColor ()";
}
}

Class Apple extends Fruit
{
// Override parents definition
Public function showColor ()
{
// But still call the parent function
Parent: showColor ();
Echo "Apple: showColor ()";
}
}

$ Apple = new Apple ();
$ Apple-> showColor ();
?>
<? Php
Class Fruit
{
Protected function showColor (){
Echo "Fruit: showColor ()";
}
}

Class Apple extends Fruit
{
// Override parents definition
Public function showColor ()
{
// But still call the parent function
Parent: showColor ();
Echo "Apple: showColor ()";
}
}

$ Apple = new Apple ();
$ Apple-> showColor ();
?>


Program running result:
Fruit: showColor ()
Apple: showColor ()

Program List: Use the scope qualifier

View plaincopy to clipboardprint?
<? 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 ();
?>
<? Php
Class Apple
{
Public function showColor ()
{
Return $ this-> 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.