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?
Asked
Active
Viewed 2.9k times
3 Answers
129
::
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();

Martin Bean
- 38,379
- 25
- 128
- 201

wildpeaks
- 7,273
- 2
- 30
- 35
-
10It should also be noted for completeness that `static` methods can be called directly from an instance in the same way that you would call "instance" method: `$obj->hello()` – prodigitalson Oct 18 '10 at 17:14
-
1@prodigitalson but note that they still behave like called static, so no $this-business xD – Hannes Oct 18 '10 at 17:25
-
@prodigitalson, I think you may have referred to this in your comment - but just to clarify a bit. Is it possible to call static members on an object instance, like so: `$obj::hello()`? My IDE is giving me some errors for variables referenced as `$this::$variableName`. – kalenjordan Jul 13 '12 at 22:45
-
1you cant call them with an object instance - you call them with a class name.. instead of `$this::methodName()` you use `self::methodName()` or `TheClass::methodName()`. As of 5.3 you could also do `$classname::methodName()` where `$classname` is a string class name. – prodigitalson Jul 14 '12 at 01:54
4
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.

prodigitalson
- 60,050
- 10
- 100
- 114
4
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.

Diod
- 41
- 1