PHP中的两个冒号意味着什么?

当我们遇到这种情况时,我不知道它在做什么:

Foo::Bar

它看起来像一条路。


这(通常)用于访问类中的静态方法或属性。 它被称为范围解析运算符,或Paamayim Nekudotayim(导致一些令人惊讶的混淆错误消息!)。 见http://www.php.net/manual/en/language.oop5.paamayim-nekudotayim.php。


::运算符是范围解析运算符。 它用于从类外部访问类常量或静态属性和方法:

ClassName::CONSTANT_VALUE
ClassName::staticMethod()

或者在类方法中使用selfparent引用相同或父类:

self::CONSTANT_VALUE
self::staticMethod()
parent::CONSTANT_VALUE
parent::staticMethod()

范围解析运算符(::)双冒号是允许访问类的静态常量和重写属性或方法的标记。

<?php
class A {

public static $B = '1'; # Static class variable.

const B = '2'; # Class constant.

public static function B() { # Static class function.
    return '3';
}

}

echo A::$B . A::B . A::B(); # Outputs: 123
?>
链接地址: http://www.djcxy.com/p/1745.html

上一篇: What do two colons mean in PHP?

下一篇: What does ":" mean in PHP?