What does -> mean in PHP?

Asked

Viewed 3,285 times

9

I’m studying a little php to use in a job and came across a snippet of code on the internet that is used "->" in the code and I could not find any page dealing with this element of code.

return $stmt->execute();
  • 1

    Good morning, did any of the answers solve your problem? If you tick the one that solved the problem correctly, if you don’t know how to do it, then read: http://meta.pt.stackoverflow.com/a/1079/3635 - I’m sure you’ll take my comment as a constructive criticism.

  • 1

    Thanks... I’m new here and I needed to figure out how to do it.

3 answers

12


(->) that operator is known informally as arrow, the manual calls him T_OBJECT_OPERATOR is used to access properties or methods of an object, for static members (those belonging/shared to the class) the :: Paamayim Nekudotayim.

Other languages like java and C# use dot instead(.) in place of (->).

10

In objects and classes, that -> (T_OBJECT_OPERATOR) is the way to access a property or method.

For example:

$obj = new StdClass;

$obj->foo = "bar";
echo $obj->foo; // vai dar "bar"

So you can use how Setter

$obj->foo = "bar";

and how getter if you do not assign any value with =.

3

Although it was asked only from the arrow operator, I think who has the doubt about this operator, will also have at least about these other two operators, as I had at the beginning:

arrow operator

-> is used to access method or class instance property [or to access method or property of an object].

$obj = new Cliente();
$obj->nome = 'Fred';
$obj->getNome();

scope resolution operator

:: Since we mentioned the arrow operator, it’s good to know there’s the operator
which is used when you want to call a static method, access a static variable, constants, and overloaded methods. It is called the Scope Resolution Operator, or is also called Paamayim Nekudotayim, or in simpler terms, two-point double.

example:

<?php
class MinhaClasse {
  const VALOR_CONST = 'Um valor constante';
}
echo MinhaClasse::VALOR_CONST;
?>

double arrow operator

Remembering that there is also the double arrow operator:

=> is used to assign values to array keys [arrays]

$meuArray = array(
  0 => 'Big',
  1 => 'Small',
  2 => 'Up',
  3 => 'Down'
);

another example:

$ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34, 1=>2);

In Java, Javascript, . NET there is no difference between ->, => and ::
In these languages you use the operator . and for arrays only [ and ]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.