php PHP中的两个冒号是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2903564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What do two colons mean in PHP?
提问by ben
I don't know what it's doing when we have this situation:
当我们遇到这种情况时,我不知道它在做什么:
Foo::Bar
It looks like a path.
它看起来像一条路径。
回答by Gumbo
The ::operator is the scope resolution operator. It is used to access class constants or static properties and methods, either from outside the class:
该::运营商范围解析操作。它用于从类外部访问类常量或静态属性和方法:
ClassName::CONSTANT_VALUE
ClassName::staticMethod()
Or within a class method to reference the same or a parent class using selfand parent:
或者在类方法中使用selfand引用相同或父类parent:
self::CONSTANT_VALUE
self::staticMethod()
parent::CONSTANT_VALUE
parent::staticMethod()
回答by Chris
That's (generally) for accessing a static method or property in a class. It's called the scope resolution operator, or Paamayim Nekudotayim (which leads to some amazingly confusing error messages!). See http://www.php.net/manual/en/language.oop5.paamayim-nekudotayim.php.
这(通常)用于访问类中的静态方法或属性。它被称为范围解析运算符,或 Paamayim Nekudotayim(这会导致一些令人惊讶的令人困惑的错误消息!)。请参阅http://www.php.net/manual/en/language.oop5.paamayim-nekudotayim.php。
回答by susheel sahoo
The Scope Resolution Operator(::)the double colon, is a token that allows access to static, constant, and overridden properties or methods of a class.
范围解析运算符 (::) 是双冒号,是一个标记,允许访问类的静态、常量和覆盖的属性或方法。
<?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
?>
回答by stalin wesley
use of Scope Resolution Operator
范围解析运算符的使用
A class constant, class property (static), class function (static) can all share the same name and be accessed using the double-colon
类常量、类属性(静态)、类函数(静态)都可以共享相同的名称并使用双冒号访问
class B{
public static $par = "par";
}
class A extends B{
const B = "constant";
public static $sel = "self";
public static $b = "static property";
public static function b(){
echo "static function";
}
public static function c(){
return parent::$par;
}
public static function d(){
return self::$sel;
}
public static function e(){
return self::$par;
}
}
echo A::B.PHP_EOL;
echo A::$b.PHP_EOL;
echo A::b().PHP_EOL;
echo A::c().PHP_EOL;
echo A::d().PHP_EOL;
回答by Slack Undertow
To supplement the answers regarding PHP's use of two colons as its "scope resolution operator":
补充有关 PHP 使用两个冒号作为其“范围解析运算符”的答案:
In addition, a double colon is used:
此外,还使用了双冒号:
To resolve an unqualified, qualified, or aliased class name to its fully qualified form, and
To invoke a class's "__callStatic" method with an arbitrary, previously undeclared method name.
将非限定、限定或别名类名称解析为其完全限定形式,以及
使用任意的、先前未声明的方法名称调用类的“__callStatic”方法。
To resolve a class name to its fully qualified form by appending "::class"
通过附加“::class”将类名解析为其完全限定的形式
Two colons followed by the "class" keyword, placed after the name of a class, provides that class's fully qualified name as a string. I.e., "ClassName::class" resolves to the fully qualified name of "ClassName". See: (A) Manual: Classes and Objects: Basics, (B) Manual: Classes and Objects: Class Constants, and (C) Manual: Language Reference: Constants
两个冒号后跟“class”关键字,放在类名之后,以字符串形式提供该类的全限定名。 即,“ClassName::class”解析为“ClassName”的完全限定名称。 请参阅:(A)手册:类和对象:基础知识,(B) 手册:类和对象:类常量,和 (C)手册:语言参考:常量
The syntax was adopted in PHP 5.5. See: (A) RFCand (B) PHP 5.5 New Features
PHP 5.5 中采用了该语法。 请参阅:(A) RFC和 (B) PHP 5.5 新功能
The "::class" syntax is useful within a namespace to obtain the fully qualified name of a class from its unqualified or qualified form, or from an aliasof its name.
"::class" 语法在命名空间中非常有用,可用于从类的非限定或限定形式,或从其名称的别名中获取类的完全限定名称。
The "::class" syntax seems to work to resolve interface names as well as class names, although that does not appear to be documented by the sources linked above.
"::class" 语法似乎可以解析接口名称和类名称,尽管上面链接的来源似乎没有记录。
Within a class, the syntax also works with "self::class", as mentioned by the "::class" RFC linked above.
在类中,语法也适用于“self::class”,如上面链接的“::class”RFC 所述。
A few examples:
几个例子:
<?php
namespace MyNamespace;
use MyNamespace\YourInterface as HerInterface;
use MyNamespace\YourClass as HerClass;
use MyNamespace\TheirClass as OurClass;
interface MyInterface { }
interface YourInterface { }
class MyClass { }
class YourClass { }
class TheirClass
{
public function fullName()
{
echo self::class;
}
}
$ourClassInstance = new OurClass;
echo MyClass::class, PHP_EOL;
// outputs: MyNamespace\MyClass
echo HerClass::class, PHP_EOL;
// outputs: MyNamespace\YourClass
echo MyInterface::class, PHP_EOL;
// outputs: MyNamespace\MyInterface
echo HerInterface::class, PHP_EOL;
// outputs: MyNamespace\YourInterface
echo $ourClassInstance->fullName(), PHP_EOL;
// outputs: MyNamespace\TheirClass
To invoke "__callStatic" with an undeclared method name
使用未声明的方法名称调用“__callStatic”
Two colons can be used to "call" a static method name that a class has not declared. E.g., "ClassName::arbitraryMethodName()". Doing so invokes the class's "__callStatic" method, if the class has declared one. It also passes to __callStatic the name of the undeclared method and any arguments passed to the undeclared method. The __callStatic method then may "dynamically" choose how to handle the call. PHP refers to this as "overloading"with the __callStatic "magic method".
两个冒号可用于“调用”类尚未声明的静态方法名称。例如,“ClassName::arbitraryMethodName()”。这样做会调用类的“__callStatic”方法,如果类已经声明了一个方法。它还将未声明方法的名称和传递给未声明方法的任何参数传递给 __callStatic。__callStatic 方法然后可以“动态地”选择如何处理调用。PHP称此为“超载”与__callStatic“魔术方法”。
See additional StackOverflow discussion
查看其他StackOverflow 讨论
Example:
例子:
<?php
namespace OurCompany\Orders;
class Intake
{
public static function __callStatic($name, $arguments)
{
$item = substr($name, 5); // trims "order" prefix
$specialistClass = "\OurCompany\Specialists\" . $item;
if (class_exists($specialistClass)) {
$specialist = new $specialistClass;
return $specialist->handleOrder($arguments);
}
return "I'm sorry, we can't help you with " .
lcfirst($item) . ".";
}
}
namespace OurCompany\Specialists;
class Car
{
public function handleOrder($arguments)
{
return "May I help you with a $arguments[0] car?";
}
}
class Truck
{
public function handleOrder($arguments)
{
return "May I help you with a $arguments[0] truck?";
}
}
use OurCompany\Orders\Intake;
echo Intake::orderCar("red"), PHP_EOL;
// outputs: May I help you with a red car?
echo Intake::orderTruck("pickup"), PHP_EOL;
// outputs: May I help you with a pickup truck?
echo Intake::orderShoes("suede"), PHP_EOL;
// outputs: I'm sorry, we can't help you with shoes.

