Functions help for started needed

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
desmi
Forum Commoner
Posts: 64
Joined: Sun Jun 15, 2008 4:55 am

Functions help for started needed

Post 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?
nowaydown1
Forum Contributor
Posts: 169
Joined: Sun Apr 27, 2008 1:22 am

Re: Functions help for started needed

Post 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.
desmi
Forum Commoner
Posts: 64
Joined: Sun Jun 15, 2008 4:55 am

Re: Functions help for started needed

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