SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Thread: $this variable?

  1. #1
    SitePoint Member
    Join Date
    Apr 2009
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question $this variable?

    can somebody guide me find information about this variable.

    it is said that this variable is used when a method is called from within an object context, as it said in manual. But why, why can't we call like that
    Code:
    <?php
    
    class This {
      private $a=1;
      
      public function get_a() {
        //return $this->a;
    	return $a;
      }
    
      
    }
    ?>
    I know this variable called pseudo-variable, I am not a programmer I hope guys can tell me why we have to use $this variable.

  2. #2
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,032
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    The $this-> is used for member variables and functions of objects.
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  3. #3
    SitePoint Member
    Join Date
    Apr 2009
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SpacePhoenix View Post
    The $this-> is used for member variables and functions of objects.
    my question is why we use this variable?

  4. #4
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,032
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    It is used to limit the scope of the variable.
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  5. #5
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    The first paragraphs and examples of the basics (of OOP in PHP 5) in the PHP manual should help clarify things.
    Salathe
    Software Developer and PHP Manual Author.

  6. #6
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    my mobile portal
    ghiris.ro

  7. #7
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    $this refers to the same as $b = new object; $b->method(); But with a higher set of access (private, protected).
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  8. #8
    SitePoint Addict
    Join Date
    Aug 2007
    Location
    St. Louis, MO.
    Posts
    206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It finally clicked with me when I started reading a piece of code like this ...

    $this->user

    ... as "this instance of user".


    Maybe that will help, I remember getting stuck on that in the begining also.

  9. #9
    SitePoint Guru
    Join Date
    Jun 2006
    Posts
    638
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $this = this, the object your in.

    $b = that, that object over there.

    Code:
    ...
    // Called from within a class, to access the variable "name" of the same class.
    $this->name = 'bob';
    ...
    // Calls the variable 'name' from the class of the variable '$b'
    $b->name = 'bob';
    ...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •