Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

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

like image 273
nectar Avatar asked Jun 14 '10 13:06

nectar


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

like image 114
Powerlord Avatar answered Nov 03 '22 16:11

Powerlord


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();
like image 30
Mark Baker Avatar answered Nov 03 '22 17:11

Mark Baker