Where do we use the object operator "->" in PHP?
The object operator "->" is used in PHP to access a property or method of an object. For example:
<?php
class Person
{
public $name;
public $age;
public function __construct($name, $age)
{
$this->name = $name;
$this->age = $age;
}
public function sayHello()
{
return "Hello, my name is " . $this->name . " and I am " . $this->age . " years old.";
}
}
$person = new Person("John", 30);
echo $person->name; // Outputs "John"
echo "\r\n";
echo $person->sayHello(); // Outputs "Hello, my name is John and I am 30 years old."
?>
Watch a video course
Learn object oriented PHP
In the example above, the object operator "->" is used to access the properties name
and age
, and the method sayHello()
of the $person
object.