Home > Backend Development > PHP Tutorial > A brief description of the usage of PHP object-oriented operator '::'

A brief description of the usage of PHP object-oriented operator '::'

巴扎黑
Release: 2023-03-07 12:00:01
Original
22434 people have browsed it

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: http://www.php.cn/php-weizijiaocheng -360326.html

The 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 = &#39;奥利安娜&#39;;                  //定义常量,用关键字const
    function __construct()                        //父类中的构造函数
    {
        echo &#39;5楼要玩中单&#39; . Hero :: CHARACTER . &#39;不给就送&#39;;     //输出常量
        echo &#39;<br/>&#39;;
    }
}
 class I_Hreo extends Hero{                      //创建子类I_Hreo继承父类
     const ROLE = &#39;亚索&#39;;                         //创建常量
     function __construct()                      //子类中创建构造函数
     {
         parent :: __construct();               //引用父类中的构造函数
         echo &#39;4楼中单&#39; . self :: ROLE . &#39;贼6&#39;; //子类中输出
     }
 }
$gamer = new I_Hreo();                             //实例化
Copy after login


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:

A brief description of the usage of PHP object-oriented operator ::

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!

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