Home Backend Development PHP Tutorial Application of double colon in php

Application of double colon in php

Nov 24, 2016 am 11:02 AM
php

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 properties in a class, which can reduce resource usage because each instance of the class will occupy part of the resources.


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 parent Method


Php 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 List: Use scope qualifier


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


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

See all articles