What does the -> do?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
typeleven
Forum Newbie
Posts: 1
Joined: Tue Jan 24, 2012 1:25 pm

What does the -> do?

Post 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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: What does the -> do?

Post 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.
Post Reply