代码之家  ›  专栏  ›  技术社区  ›  mko

PHP中双冒号和箭头运算符的区别?[副本]

php
  •  55
  • mko  · 技术社区  · 15 年前

    link text addChild方法使用 :: 作用域解析运算符,但在示例中使用箭头运算符。有人能告诉我为什么吗?

    3 回复  |  直到 12 年前
        1
  •  113
  •   Martin Bean    13 年前

    :: 用于静态元素 -> 是例如元素。

    class Example {
      public static function hello(){
        echo 'hello';
      }
      public function world(){
        echo 'world';
      }
    }
    
    // Static method, can be called from the class name
    Example::hello();
    
    // Instance method, can only be called from an instance of the class
    $obj = new Example();
    $obj->world();
    

    More about the static concept

        2
  •  4
  •   prodigitalson    15 年前

    这只是表示它是一个对象的方法,与实际使用无关。

    $object 所以 -> 运算符不正确,因为您要列出实际的类名。因此,在使用静态方法(类名是静态的)之后,可以使用scope res.operator :: ...

        3
  •  3
  •   Diod    15 年前

    箭头表示addChild作为对象的成员调用(在本例中为$sxe)。

    双冒号表示addChild是SimpleXMLElement类的成员。

    推荐文章