Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Difference between -> and :: [duplicate]

Possible Duplicate:
In PHP, whats the difference between :: and ->?

In PHP, what is the main difference of while calling a function() inside a class with arrow -> and Scope Resolution Operator :: ?

For more clearance, the difference between:

$name = $foo->getName();
$name = $foo::getName();

What is the main profit of Scope Resolution Operator :: ?

like image 442
夏期劇場 Avatar asked Sep 23 '11 05:09

夏期劇場


People also ask

What does -> represent in PHP?

The object operator, -> , is used in object scope to access methods and properties of an object. It's meaning is to say that what is on the right of the operator is a member of the object instantiated into the variable on the left side of the operator.

What is the difference between double colon and arrow in 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.

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

-> and => are both operators. The difference is that => is the assign operator that is used while creating an array.

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

== Operator: This operator is used to check the given values are equal or not. If yes, it returns true, otherwise it returns false. === Operator: This operator is used to check the given values and its data type are equal or not. If yes, then it returns true, otherwise it returns false.


1 Answers

$name = $foo->getName();

This will invoke a member or static function of the object $foo, while

$name = $foo::getName();

will invoke a static function of the class of $foo. The 'profit', if you wanna call it that, of using :: is being able to access static members of a class without the need for an object instance of such class. That is,

$name = ClassOfFoo::getName();
like image 71
K-ballo Avatar answered Nov 07 '22 05:11

K-ballo