What does it mean - in PHP?

8

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

return $stmt->execute();
    
asked by anonymous 05.10.2015 / 23:39

3 answers

11

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

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

    
05.10.2015 / 23:46
9

In objects and classes , this -> ( 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 setter

$obj->foo = "bar";

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

    
05.10.2015 / 23:46
1

Although it was asked only by the arrow operator, I think anyone who has doubts about this operator will also have at least two other operators, as I had in the beginning:

operator arrow

-> 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 is good to know that there is the operator
which is used when you want to call a static method, access a static variable, constants, and overloaded methods. It is called a Scope Resolution Operator, or it is also called Paamayim Nekudotayim, or in simpler terms, double colons.

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 ]

    
29.04.2018 / 22:02