Home >Backend Development >PHP Tutorial >A brief description of the usage of PHP object-oriented operator '::'
What is the operator "::"?
The operator "::" is more powerful than the pseudo variable $this which can only be used inside the class. The operator "::" can access member methods or member variables in a class without any declaration of any instance.
Operator "::" syntax:
The general syntax for using the "::" operator is:
Keywords: :Variable name/constant name/method name
##Note:
Here The keywords are no longer public, private, protected or static, etc., but the following three situations: (1) parent: You can call member variables, member methods and constants in the parent class; (2) self: You can call static members and constants in the current class; (3) Class name: You can call variables, constants and methods in this class.Friendly reminder:
For related content on static variables (methods), please refer to the article on this site: //m.sbmmt.com/php-weizijiaocheng -360326.htmlThe above briefly introduces the basic content of the operator "::". Below we use a simple example to understand the usage of the operator "::" more vividly and specifically.<?php header("content-type:text/html;charset=utf-8"); class Hero{ //定义类 const CHARACTER = '奥利安娜'; //定义常量,用关键字const function __construct() //父类中的构造函数 { echo '5楼要玩中单' . Hero :: CHARACTER . '不给就送'; //输出常量 echo '<br/>'; } } class I_Hreo extends Hero{ //创建子类I_Hreo继承父类 const ROLE = '亚索'; //创建常量 function __construct() //子类中创建构造函数 { parent :: __construct(); //引用父类中的构造函数 echo '4楼中单' . self :: ROLE . '贼6'; //子类中输出 } } $gamer = new I_Hreo(); //实例化
Instance explanation:
In the above example, we first created a parent class Hero , defined constants, created a constructor, and then created a subclass I_Hreo to inherit the parent class Hreo, and also defined constants in the subclass. The next step is the key point, we refer to the constructor in the parent class. We introduced three situations above. Parent: You can call member variables, member methods and constants in the parent class. Here we are calling the method in the parent class, so we use parent::__construct(). Next, we output the constants defined in the subclass in the constructor of the subclass and call the keyword of the current class. As introduced above, self: can call static members and constants in the current class, so here we use self:: ROLE, finally format and run it. The results are shown in the figure below: The above is a brief description of the usage of operator '::', I hope it can help everyone.The above is the detailed content of A brief description of the usage of PHP object-oriented operator '::'. For more information, please follow other related articles on the PHP Chinese website!