SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: What is $this->

  1. #1
    SitePoint Member
    Join Date
    Jun 2006
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question What is $this->

    This may sound like a very simple and basic question, but here it is. I have a script I downloaded and want to integrate it into my site. I'm in the process of looking over the code and all thoughout it I am seeing stuff like $this->$variablename or $this->functionname(). Would this (no pun intended) be similar to $this= or what exactly does $this-> do. I've tried searching around and can't find any mention of it on any PHP documentation.

    Thanks

    C

  2. #2
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    $this-> has to do with Object Orientated Programming.

    Basically, you have classes, which are definitions for objects. An object is something you can use in your script.. A very simple example:

    PHP Code:
    class User {

        var 
    $name;

        function 
    setName($tmp_name) {
            
    $this->name $tmp_name;
        }

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

    OK, that's a very simple class. Now, to use this:

    PHP Code:
    // let's include the class
    include "user.class.php";

    // let's create a User object (this uses the class file as blueprint)
    $member = new User;

    // let's assign a name. note the => thingy. This means we're using a function within the object
    $member->setName('Bob');

    // now we can print the name
    echo $member->getName(); 
    In the second bit of code, we use $member-> to specify that we're using a function within the $member object.

    In the first bit of code, you can see a $this->
    That basically means that you want to do something within the object itself.

    So:

    $this->someFunction();
    means a function within an object is being called.

    $this->someVariable;
    means a variable within the object is being used.


    You can read much more on this on the PHP site: http://www.php.net/oop


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
  •