Difference between double colon and arrow operators in PHP?

Php

Php Problem Overview


In the sidebar of the php web manual, link text the addChild method uses the :: scope resolution operator, but in the example it uses the Arrow operator. Can anyone tell me why that is?

Php Solutions


Solution 1 - Php

:: is for static elements while -> is for instance elements.

For example:

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

Solution 2 - Php

This is just notation for the fact that its the method of an object and has nothing to do with actual usage.

In the case of documentation youre not dealing with an instance of an object like $object so the -> operator wouldnt be correct since you want to list the actual class name. So following the usage for a static method where the class name is static you use the scope res. operator ::...

This is generally how php documentation works for classes.

Solution 3 - Php

The arrow means the addChild is called as a member of the object (in this case $sxe).

The double colon means that addChild is a member of the SimpleXMLElement class.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmkoView Question on Stackoverflow
Solution 1 - PhpwildpeaksView Answer on Stackoverflow
Solution 2 - PhpprodigitalsonView Answer on Stackoverflow
Solution 3 - PhpDiodView Answer on Stackoverflow