Page 1 of 1

Functions help for started needed

Posted: Tue Aug 12, 2008 12:41 pm
by desmi
Ok, i've tried my best with Google, no answers.

Question: What does $this->... mean in functions? i've seen some functions with these, and cant understand what they do.. Is there a proper guide to functions where i could find out more about it ? or is it even only function related thing?

Re: Functions help for started needed

Posted: Tue Aug 12, 2008 1:18 pm
by nowaydown1
Hi des,

The $this-> operator that you're seeing is used to reference variables when you're in an object. Take the following procedural example:

Code: Select all

 
<?php
function getUserName() {
    $name = "bob";
    return $name;
}
?>
 
In an object oriented setup, it would be something more like this probably:

Code: Select all

 
<?php
class User {
    private $name;
 
    public function setName($name) {
        $this->name = $name;
    }
 
    public function getName() {
       return $this->name;
    }
}
?>
 


You can see that 'User' class has one private member variable called $name. In order to access that variable, I have to use $this->. To clarify even more, try googling around for php object oriented programming or something similar. Hope that makes sense.

Re: Functions help for started needed

Posted: Tue Aug 12, 2008 1:35 pm
by desmi
Thanks for fast reply!

I really need to get more information about all of that stuff, i'm not too familiar with classes yet ;)
I'll try to google these object/class things.

All examples and information here is very welcome!