Jump to content

PHP $this


lucerias

Recommended Posts

$this refers to the class itself.

Example:

[code]
<?php
class A
{
  private $var;

  public function getVar()
  {
    return $this->var;
  }
}
?>
[/code]

It can also be used to call a function in the same class with same syntax.  Say you have a function called setVar that needs to be called internally, you could do $this->setVar();

This is a very brief rundown but I hope it helps,

Dest
Link to comment
Share on other sites

More specifically it refers to an instance of a class.

If you create two class objects, then $this will tell you which instance is being used.

[code=php:0]$first = new A;
$second = new A;
$first->getVar();
$second->getVar()[/code]


How does the class know which $var to get?  When you call $first->getVar(), $this->var is the $var from $first.  When you call $second->getVar(), $this->var is the $var from $second.
Link to comment
Share on other sites

[quote]<?php
class A
{
  private $var;

  public function getVar()
  {
    return $this->var;
  }
}
?>


It can also be used to call a function in the same class with same syntax.  Say you have a function called setVar that needs to be called internally, you could do $this->setVar();

This is a very brief rundown but I hope it helps,

Dest[/quote]

Then may i know what is the usage of private under a class and the meaning of public and non public function. Thank you.
Link to comment
Share on other sites

[code]<?php
class Foo
{
    function Variable()
    {
        $name = 'Bar'; 
        $this->$name(); // This calls the Bar() method
    }
   
    function Bar()
    {
        echo "This is Bar";
    }

}

$foo = new Foo();
$funcname = "Variable";
$foo->$funcname();  // This calls $foo->Variable()

?> [/code]

I tried to add this Foo::Variable(); before ?> and it caused an error. May i know why? If i remove $this->$name(); then the error is fixed. If Foo::Variable(); can call the function directly, why we need to code like this:

$foo = new Foo();
$funcname = "Variable";
$foo->$funcname();  // This calls $foo->Variable()

What is the difference? Thank you.
Link to comment
Share on other sites

You can actually call it like this:
[code]
$foo=new Foo();
$foo->Variable();
[/code]

As to not using Foo::Variable(), then it does not have a $this var that is passed to the function, so it will yell at you for referencing $this because it is not defined. Foo::Variable() is for calling static functions that do not require an instance of the class, and cannot reference the current class using $this because there is not current class that it is under.

Monkeymatt
Link to comment
Share on other sites

lucerias, try working with an example which creates two instances of a class, AND which stores data within the class.  Then it should be clear why Foo::Variable() cannot use $this.

While you are working with class methods only, it won't be clear why there is a difference.
Link to comment
Share on other sites

What existing class? There is no instance of the class until you create a variable and define it as of that class. There is only a definition of the class. Think of it this way: what we have before we defined a variable to be the class is like blueprints to a house - you know what it should look like and what it will do when it is made, but it is not made yet. After the variable is defined and set to be the class, it is like the house is built and is now there and you can now do things with it.

Monkeymatt
Link to comment
Share on other sites

Another way of thinking of is....like a till for a register
Like at grocery store, everyone has their own till/register drawer
and each person's drawer begins with a set amount of bills.

You could call that the Till Class
It has 100 dollars
-10 Ones
- 10 Fives
- 2 Tens
- 2 Twenties

When the shift starts there are probably 5 employees that get a new till
so

$beth = new Till;
$john = new Till;
$henry = new Till
$zane = new Till;
$bob = new Till;

5 new instances of a Till
and with each Till you can...... takeFiveDollars()
or takeTenDollars()

$beth->takeTenDollars();
$zane->takeTenDollars();
$zane->takeOneDollar();
$zane->takeTwentyDollars();
$john->giveFiveDollars();

eventually each person's set amount of bills will change
and that is the theory behind objects and classes.....all the employees are objects of Till
Link to comment
Share on other sites

Thank you, i am clear with your example. Another problem of class and function is i tried to create a sum relationship to understand more about class and function.

The "First" coding shown as below can work properly whereas i have another idea is define the $val1 and $val2 in function variable() and then $val3 in function foo(), so the calculation will be done in function foo() by passing the value of $val1 and $val2 from variable() to foo(). The "Second" coding is what i have tried to do but there is error, please help me to resolve it. Thank you.

First
[code]<?php

$val1=1;
$val2=2;
$val3=0;

class Foo

    function Variable()
    { 
        global $val1; 
        global $val2;
        global $val3;
       
        $val3=$val1+$val2;
       
        $name = 'Bar';               
        $this->$name(); // This calls the Bar() method
    }
   
    function Bar()
    { 
        global $val3;     
        echo $val3;
        echo "This is Bar";
    }
}

$foo = new Foo();
$funcname = "Variable";

?> [/code]



Second
[code]<?php

class Foo

    function Variable()
    { 
        $val1=1;
        $val2=2;

        $name = 'Bar';               
        $this->$name(); // This calls the Bar() method
    }
   
    function Bar()
    { 
        global $val1;
        global $val2;
        $val3=0;
       
        $val3=$val1+$val2;
         
        echo $val3;
        echo "This is Bar";
    }
}

$foo = new Foo();
$funcname = "Variable";

?> [/code]

Link to comment
Share on other sites

you can't grab the scope of another function with global

global is meant to extend the scope of variables outside all function and classes


I don't exactly know what your question is but I can tell you you're not going to be able to grab val1 and val1 from Variable() by asking for globals in Bar()

take a gander at this page and research globals
http://us3.php.net/global
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.