How are people able to use the arrow operator like this:
$this->something->something();
How are people able to use the arrow operator like this:
$this->something->something();
It’s used in object oriented code and can refer to a method or property of an object.
class person {
public $name;
};
$john = new person();
$john->name = 'John';
echo $john->name;
<WhiteVAL> Return: John
class one
{
public $one = "I'm in one<br />";
public function test()
{
echo "I'm in one too";
}
}
class two
{
private $d;
public function __construct()
{
$this->d = new one();
echo $this->d->one;
$this->d->test();
}
}
$two = new two();
You can also daisy chain methods by returning $this.
class Test
{
public function foo()
{
return $this;
}
public function bar()
{
return $this;
}
}
$test = new Test();
$test->foo()
->bar()
->foo()
->bar()
->foo()
->bar();
or even:
class one
{
public $one = "I'm in one<br />";
public function test()
{
echo "I'm in one too";
}
}
class two
{
private $d;
public function __construct(one $d)
{
$this->d = $d;
echo $this->d->one;
$this->d->test();
}
}
$one = new one();
$two = new two($one);
Thank you all so much