$this->

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
Stela
Forum Commoner
Posts: 46
Joined: Tue Aug 12, 2008 12:38 pm

$this->

Post by Stela »

~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Hello...

I'm learning PHP with the help of a book, but I can't full understand the operator $this-> (how it works):

Code: Select all

 
<?php
    
    class Cat
        {
        var $age;
        
        function Cat($new_age)
            {
            $this->age = $new_age;
            }
 
        function Birthday()
            {
            $this->age++;
            }
        }
 
 
 
        $fluffy=new Cat();
        echo "Age is $fluffy->age <br/>";
        echo "Birthday<br/>";
 
        $fluffy->Birthday();
        echo "Age is $fluffy->age <br/>";
        
    ?>
 
Can someone explain it to me or give me a link to a tutorial?

Thanks :D


~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
User avatar
panic!
Forum Regular
Posts: 516
Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK

Re: $this->

Post by panic! »

$this access that specific instance of a class.


if we have a class cat, with a function get_name(); like this

Code: Select all

 
 
class cat{
public $name;
 
function get_name()
{
 return $this->name;
}
 
}
 
$cat1=new cat();
 
$cat2=new cat();
 
// here we have two instances of the class cat;
 
$cat1->name='Timmy';
$cat2->name='Jessie';
 
//now we've given them both names
 
 
print $cat1->get_name(); // this will print Timmy
 
print $cat2->get_name(); // this will print Jessie
 
 
 
 

if you need further help or a better explaination; don't hesitate to ask.
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: $this->

Post by Ziq »

Stela
Forum Commoner
Posts: 46
Joined: Tue Aug 12, 2008 12:38 pm

Re: $this->

Post by Stela »

Thanks for the link Ziq.



panic!:

When:

$cat1 -> name = 'Timmy';

Where is Timmy stored?
User avatar
panic!
Forum Regular
Posts: 516
Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK

Re: $this->

Post by panic! »

<?

class cat{
public $name; <--- HERE

function get_name()
{
return $this->name;
}

}

?>

It's stored within $cat1.

$cat1 is an instance of a class. It has it's own variables that belong to it, when you do $this->name='Timmy' you're giving the variable 'name' that belongs to cat1 a value of 'Timmy'.

you could have loads of variables that belong to a class such as.

Code: Select all

 
class cat
{
public $age;
public $weight;
public $name;
public $breed;
function meow()
{
print "meow my name is ".$this->name." i am a ".$this->breed;
}
}
 
 
$cat1=new cat();
$cat1->weight=10;
$cat1->name="Timmy";
$cat1->breed="Tabby";
 
$cat2=new cat();
$cat2->name="Dave";
$cat1->breed="Black and White Cat";
 
 
 
$cat1->meow(); // this will say "meow my name is Timmy i am a Tabby";
 
$cat2->meow(); // this will say "meow my name is Dave i am a Black and White Cat";
 
 


so $cat1 and $cat2 are separate objects with their own variables and methods. Think of them as containers.

Google 'Object Oriented Programming' for more info. Or ask me if you need more help.
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: $this->

Post by Ziq »

Stela wrote: $cat1 -> name = 'Timmy';

Where is Timmy stored?

Code: Select all

 
class cat
{
    public $name;
 
    function get_name()
    {
       return $this->name;
    } 
}
$cat1=new cat();
$cat1 -> name = 'Timmy'; // stored in member $name (public name)
 
If you want to change member outside class $object->member ($cat1 -> name). Inside class you must use special pseudo-variable $this-> ($this->name).

Thus

Code: Select all

 
echo $cat1->name;
echo $cat1->get_name();
 
use same member
Stela
Forum Commoner
Posts: 46
Joined: Tue Aug 12, 2008 12:38 pm

Re: $this->

Post by Stela »

Now I think I get it :D

I think there's only an error on the code

$cat2=new cat();
$cat2->name="Dave";
$cat1->breed="Black and White Cat";

$cat1->meow(); // this will say "meow my name is Timmy i am a Tabby";
$cat2->meow(); // this will say "meow my name is Dave i am a Black and White Cat";


The third $cat1 should be $cat2, right?

Thank you all for the enlightenment!
User avatar
panic!
Forum Regular
Posts: 516
Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK

Re: $this->

Post by panic! »

Whoops yeah you're right!


The code should run anyway though! but it may give you a 'notice' error warning you breed for cat2 is blank.
Post Reply