What are the different ways where we can use object operators ->
in 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.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With