How are people able to use the arrow operator like this:
PHP Code:$this->something->something();
| SitePoint Sponsor |
How are people able to use the arrow operator like this:
PHP Code:$this->something->something();
It's used in object oriented code and can refer to a method or property of an object.
Code:class person { public $name; }; $john = new person(); $john->name = 'John'; echo $john->name; <WhiteVAL> Return: John
PHP Code: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();
Last edited by Ernie1; Jan 3, 2009 at 16:54.
my mobile portal
ghiris.ro



You can also daisy chain methods by returning $this.
PHP Code:class Test
{
public function foo()
{
return $this;
}
public function bar()
{
return $this;
}
}
$test = new Test();
$test->foo()
->bar()
->foo()
->bar()
->foo()
->bar();
Brad Hanson, Web Applications & Scalability Specialist
► Is your website outgrowing its current hosting solution?
► PM me for a FREE scalability consult!
► USA Based: Available by Phone, Skype, AIM, and E-mail.
or even:
PHP Code: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);
my mobile portal
ghiris.ro
Thank you all so much![]()
Bookmarks