Application of double colon in php

巴扎黑
Release: 2023-03-02 16:16:02
Original
1244 people have browsed it

The "::" operator is often seen in PHP class codes. This is a scope limiting operator, which is represented by a double colon "::". It is used to set the levels of different scopes in the class. The left side is the scope and the right side is the members of the access scope.

There are two scopes defined in php: self and parent (static scope is provided in php6).

self: Represents the scope of the current class. Unlike this, it does not represent a specific instance of the class. Self cannot be used in code outside the class, and it cannot identify its own hierarchical position in inheritance. That is to say, when self is used in an extended class, it does not call the method of the parent class, but the overloaded method of the extended class.

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

Example of PHP double colon::operator:

<?php
class forasp{
  static $url="http://blog.csdn.net/abandonship";
  static $webname = "PHP学习之双冒号的用法";
  public function writeurl(){
    echo self::$url;//调用自己的内容
  }
  public function writewebname(){
    echo "测试子类调用父类内容";
  }
}
class cn extends forasp{
  function father(){
    parent::wirtewebname();
  }
}
$a = new forasp();//实例化父类
$a->writeurl();//调用自身内容
$b = new cn();
$b->writewebname();//调用父类内容
?>
Copy after login

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

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

The "double colon operator", also known as the "Scope Resolution Operator", can access static, const, and overridden properties and methods in classes.
If used outside the class definition, use the class name to call. In PHP 5.3.0, you can use variables instead of class names.

Program List: Use variables to access outside the class definition

<?php
class Fruit {
  const CONST_VALUE = &#39;Fruit Color&#39;;
}
$classname = &#39;Fruit&#39;;
echo $classname::CONST_VALUE; // As of PHP 5.3.0
echo Fruit::CONST_VALUE;
?>
Copy after login

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

<?php  
class Fruit {  
  const CONST_VALUE = &#39;Fruit Color&#39;;  
}  
class Apple extends Fruit  
{  
  public static $color = &#39;Red&#39;;  
  public static function doubleColon() {  
    echo parent::CONST_VALUE . "\n";  
    echo self::$color . "\n";  
  }  
}  
Apple::doubleColon();  
?>
Copy after login


Program running results:

Fruit Color Red

Program List: Call the Parent method hPhp code

<?php  
class Fruit  
{  
    protected function showColor() {  
        echo "Fruit::showColor()\n";  
    }  
}  
  
class Apple extends Fruit  
{  
    // Override parent&#39;s definition  
    public function showColor()  
    {  
        // But still call the parent function  
        parent::showColor();  
        echo "Apple::showColor()\n";  
    }  
}  
  
$apple = new Apple();  
$apple->showColor();  
?>
Copy after login


program running result:

fruit :: showcolor ()

Apple :: showcolor ()


Program:
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();  
?>
Copy after login

Program running result:

Banana is yellow

Program List: Method of calling base class

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"!  
  
?>
Copy after login



Program running result:
show color

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!