Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between double colon and arrow operators in PHP? [duplicate]

Tags:

php

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?

like image 344
mko Avatar asked Oct 18 '10 17:10

mko


People also ask

What is the use of :: in PHP?

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.

What is the difference between -> and => in PHP?

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.

What is the arrow in PHP?

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.

What is double colon in laravel?

Simply it is token which allow access to static, constant and overridden properties of method of a class.


1 Answers

:: 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

like image 59
wildpeaks Avatar answered Sep 30 '22 18:09

wildpeaks