开发者

What do two colons mean in PHP?

I don't know what it's doing when w开发者_运维问答e have this situation:

Foo::Bar

It looks like a path.


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 self and parent:

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


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.


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
?>


To supplement the answers regarding PHP's use of two colons as its "scope resolution operator":

In addition, a double colon is used:

  1. To resolve an unqualified, qualified, or aliased class name to its fully qualified form, and

  2. To invoke a class's "__callStatic" method with an arbitrary, previously undeclared method name.

To resolve a class name to its fully qualified form by appending "::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

The syntax was adopted in PHP 5.5. See: (A) RFC and (B) PHP 5.5 New Features

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 alias of its name.

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.

Within a class, the syntax also works with "self::class", as mentioned by the "::class" RFC linked above.

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

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".

See additional StackOverflow discussion

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.


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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜