Where do we use the object operator "->" in PHP?

Php

Php Problem Overview


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

Php Solutions


Solution 1 - Php

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.

Solution 2 - Php

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();

Solution 3 - Php

Call a function:

$foo->bar();

Access a property:

$foo->bar = 'baz';

where $foo is an instantiated object.

Solution 4 - Php

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();

Solution 5 - Php

"->" 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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionnectarView Question on Stackoverflow
Solution 1 - PhpPowerlordView Answer on Stackoverflow
Solution 2 - PhpMark BakerView Answer on Stackoverflow
Solution 3 - PhpmmattaxView Answer on Stackoverflow
Solution 4 - PhpWind ChimezView Answer on Stackoverflow
Solution 5 - Php李沣泉View Answer on Stackoverflow