PHP: Example code sharing of range parsing operator (::)

黄舟
Release: 2023-03-12 11:36:02
Original
1325 people have browsed it

Scope parsingThe operator (::) is a pair of colons that can be used to access static members, methods and constants , as well as members and methods in the overridden class.

When using the :: notation outside a class to access these static members, methods, and constants, the name of the class must be used, as shown in the following example.

:: Example of accessing static members and methods

<?php
Class Person{
    // 定义静态成员属性
    public static $country = "中国";
    // 定义静态成员方法
    public static function myCountry() {
        //内部访问静态成员属性
        echo "我是".self::$country."人<br />";
    }
}

// 输出静态成员属性值
echo Person::$country."<br />";
// 访问静态方法
Person::myCountry();
?>
Copy after login

Use the :: symbol to access static members and methods. For more information, please refer to "PHP Static (static)".

:: Example of accessing members and methods covered by the parent class

class Person {
    var $name;
    var $sex;
    var $age;

    function say() {
        echo "我的名字叫:".$this->name."<br />";
	echo "性别:".$this->sex."<br />";
	echo "我的年龄是:".$this->age;
    }
}
class Student extends Person {
    var $school;
	
    function say() {
        parent::say();
        echo "我在".$this->school."上学";
    }
}
Copy after login

The above is the detailed content of PHP: Example code sharing of range parsing operator (::). 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!