What are the different ways where we can use object operators ->
in PHP?

- 30,738
- 21
- 105
- 131

- 9,525
- 36
- 78
- 100
-
10Why is this closed? People are upvoting this question 8 years later, and there are sufficient answers to the question. Clearly, it's pretty easy to see what's being asked here. – DeltaFlyer Nov 16 '18 at 22:12
-
4@DeltaFlyer Vote for reopening this question – Stephan Apr 20 '20 at 11:20
6 Answers
PHP has two object operators.
The first, ->
, is used when you want to call a method on an instance or access an instance property.
The second, ::
, is used when you want to call a static
method, access a static
variable, or call a parent class's version of a method within a child class.

- 87,612
- 17
- 125
- 175
-
4Is there a name for the "->" operator ? I guess the "::" I would just call it double colon.. but this one "->" how would I call it? property accessor operator ? – Pablo Camara Nov 28 '20 at 15:11
-
5@PabloCamara The first one is called the object operator and the second the class operator. I couldn't imagine someone calling something the "double colon". – user904963 Jan 31 '22 at 20:07
When accessing a method or a property of an instantiated class
class SimpleClass
{
// property declaration
public $var = 'a default value';
// method declaration
public function displayVar() {
echo $this->var;
}
}
$a = new SimpleClass();
echo $a->var;
$a->displayVar();

- 209,507
- 32
- 346
- 385
-
8like object->method_name(); or object->prop_name; it means its more like dot(.) operator to access class methods and attributes. – nectar Jun 14 '10 at 13:26
-
2Similar to the . class operator in java, yes... but see the PHP class documentation for details – Mark Baker Jun 14 '10 at 13:29
Call a function:
$foo->bar();
Access a property:
$foo->bar = 'baz';
where $foo
is an instantiated object.

- 27,172
- 41
- 116
- 149
It is used when referring to the attributes of an instantiated object. e.g:
class a {
public $yourVariable = 'Hello world!';
public function returnString() {
return $this->yourVariable;
}
}
$object = new a();
echo $object->returnString();
exit();

- 5
- 4

- 1,166
- 1
- 9
- 19
"->" operator is the PHP related callable content. always use to call an instance method and access instance.
"::" scope operator is used for the instance that is used for calling the static method and constant it's very different with::
It's a proper reply to them, I have got new knowledge.
Please check the name conflicts for the above different operator.

- 11
- 1
arrow operator(->): It is an access operator used to access data members and methods in a class in PHP. It is the same as the (.) operator which we use in javascript, c++.

- 1
- 2
-
1As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – jv-k Jan 16 '23 at 15:33