Usage of double colon:: in PHP

不言
Release: 2023-03-23 09:52:02
Original
4962 people have browsed it

The content in this article introduces the usage of double colon in PHP. Now share it with everyone, friends in need can also refer to it

Double colon operator: that is, the scope-limited operator Scope Resolution Operator can access static, const and class Overridden properties and methods.

1. When calling static properties and static methods

::With- >The function is the same, but the objects used are different! ::Refer to static methods or properties in the class, and do not need to be instantiated!

Create a class, and create a static property $a and a static method b, as follows:




##[php] view plain copy


  1. #class test {

  2. static public $a;

  3. static public function b() {}

  4. }

If you want to call the static property $a, as follows



##[php] view plain copy


  1. #test::$a;

  2. Note that a needs to be preceded by a $ sign, which is different from the -> sign!

If you want to call static method b, as follows





##[php]

view plain

copy


    ##test::b();

##2. When calling the properties or methods of the own class or the parent class

First create a parent class



#[php] view plain copy


  1. ##class test {

  2. public function b () {}

  3. }

Then create a subclass inheritance father




[php]

view plain copy


  1. ##class

    tests extends test {

  2. public function cs() {}

  3. }

When we need to call method b of the parent class



#[php] view plain copy


  1. ##parent::b();

When we need When calling its own method cs, there are two methods



#[php]

view plain

copy


    #$this
  1. ->cs();



[php] view plain copy


  1. self::cs();  

The above is the detailed content of Usage of double colon:: in PHP. For more information, please follow other related articles on the PHP Chinese website!

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