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?
In PHP, the double colon :: is defined as Scope Resolution Operator. It used when when we want to access constants, properties and methods defined at class level. When referring to these items outside class definition, name of class is used along with scope resolution operator.
Conclusion. The two operators, => and -> may look similar but are totally different in their usage. => is referred to as double arrow operator. It is an assignment operator used in associative arrays to assign values to the key-value pairs when creating arrays.
Arrow functions are introduced as an update in the PHP version 7.4. Arrow functions are supposed to be a more concise version of anonymous functions. Arrow functions can be seen as shorthand functions that automatically inherit the parent scope's variables.
Simply it is token which allow access to static, constant and overridden properties of method of a class.
::
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With