What does "->" signify in php

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
swraman
Forum Commoner
Posts: 58
Joined: Thu Nov 06, 2008 12:33 am

What does "->" signify in php

Post by swraman »

Some code Im working with uses the line

pg_query($this->link, $query)

What does the $this->link signify?

Thanks
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: What does "->" signify in php

Post by requinix »

-> is for accessing variables and functions from an object.
$this is a special variable that refers to the class that the code is located in.

As usual, the manual is great for answering your PHP-related questions.

Code: Select all

class A {
    function x() { echo get_class($this); }
}
 
class B {
    var $b = 123;
}
 
$a = new A;
$a->x(); // prints "A"
$b = new B;
echo $b->b; // prints "123"
swraman
Forum Commoner
Posts: 58
Joined: Thu Nov 06, 2008 12:33 am

Re: What does "->" signify in php

Post by swraman »

OK thank you.

I think I understand what $this does, but I am not sure...

What does $this do? What is the difernce between $this->$whatever and just saing $watever?

thank
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: What does "->" signify in php

Post by requinix »

swraman wrote:What is the difernce between $this->$whatever and just saing $watever?
$this->whatever is the "whatever" variable that's part of the current class. $whatever is just a variable in a function.
Post Reply