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();
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();
( ->
) 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 ( ->
).
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 =
.
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:
->
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();
::
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;
?>
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 ]