Home  >  Article  >  Backend Development  >  PHP: Example code sharing of range parsing operator (::)

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

黄舟
黄舟Original
2017-07-02 11:41:051326browse

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();
?>

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."上学";
    }
}

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!

Statement:
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