Page 1 of 1

What does the -> do?

Posted: Tue Jan 24, 2012 1:31 pm
by typeleven
Ive been reading about the basics of PHP and and trying to understand some code but I keep seeing things like this...

Code: Select all

$result = $findCommand->execute()
or

Code: Select all

$result->getMessage()
Im not sure what that is called so I cant find any articles explaining what the -> is doing.

Re: What does the -> do?

Posted: Tue Jan 24, 2012 2:21 pm
by Celauran
It accesses the related object's properties and methods.

Code: Select all

class Foo
{
    public $bar;
    
    public function __construct($bar)
    {
        $this->bar = $bar;
    }
    
    public function setBar($new_value)
    {
        $this->bar = $new_value;
    }
}

$my_object = new Foo('Tomato!');
echo $my_object->bar;   // Outputs 'Tomato!'
$my_object->setBar('And now for something completely different');   // changes the value of bar
echo $my_object->bar;   // Outputs 'And now for something completely different'
Here's some suggested reading.