0

可能重复:
参考 - 这个符号在 PHP 中是什么意思?

在 PHP 中是什么::意思?例如

Pagination::set_config($config);

是否类似于=>

4

4 回答 4

5

它被称为范围解析运算符。

http://php.net/manual/en/keyword.paamayim-nekudotayim.php

于 2011-12-28T14:46:37.140 回答
4

在 PHP 中,它是范围解析运算符。它用于访问未启动类的方法和属性。对此表示法明确的方法称为静态方法

此外,您可以使用此表示法相对(从您所在的位置)遍历扩展类。例子:

class betterClass extends basicClass {
    protected function doTheMagic() {
       $result = parent::doTheMagic();
       echo "this will output the result: " . $result;
       return $result;
    }
}

在此示例中,doTheMagic 方法覆盖了其父方法的现有方法,但parent::doTheMagic();仍可以调用原始方法。

于 2011-12-28T14:47:59.853 回答
1

这种“::”-syntax 被称为Scope Resolution Operator

它用于引用基类或还没有任何实例的类中的函数和变量。

来自 php.net 的示例:

<?php
class A {
    function example() {
        echo "I am the original function A::example().<br />\n";
    }
}

class B extends A {
    function example() {
        echo "I am the redefined function B::example().<br />\n";
        A::example();
    }
}

// there is no object of class A.
// this will print
//   I am the original function A::example().<br />
A::example();

// create an object of class B.
$b = new B;

// this will print 
//   I am the redefined function B::example().<br />
//   I am the original function A::example().<br />
$b->example();
?>

只需阅读示例中的注释即可。有关更多信息,请转到php.net 文章

于 2011-12-28T14:51:23.973 回答
0

::是一个范围解析运算符(最初在 C++ 中如此命名)意味着您将set_config($config)方法与类相关联Pagination。它是一个静态方法,静态方法不能通过它的类的对象访问,因为它们与它们的类相关联,而不是与该类的对象相关联。

Pagination::set_config($config);

符号 -> 用于访问实例成员。该符号=>与 PHP 中的关联数组一起使用以访问这些数组的成员。

于 2011-12-28T14:47:14.527 回答