[RESOLVED] php class "this" or "self" keyword
Results 1 to 5 of 5

Thread: [RESOLVED] php class "this" or "self" keyword

  1. #1
    Member
    Join Date
    Nov 2007
    Posts
    58

    resolved [RESOLVED] php class "this" or "self" keyword

    hi, this sounds very embarassing but i've been using too much $this->field keyword for so long i can't remember what's the difference between "this" and "self" for php.

    does "self" has any special functionality besides being called differently? surely there must've been some purpose.

    example,
    instead of $this->GetMyName(), it becomes self::GetMyName();

    ?

  2. #2
    PHP Witch laserlight's Avatar
    Join Date
    Apr 2003
    Location
    Singapore
    Posts
    13,907
    Use $this to refer to the current object. Use self to refer to the current class. In other words, use $this->member for non-static members, use self::$member for static members.

    EDIT:
    Oh wait, I recall that parent::member() is used to call a base class member function. Consequently, you could also use self::member() to call a non-static member function, though in most cases it would be better to use $this->member() so that polymorphism will work. As such, unless you specifically do not want a derived class member function override to work, use $this->member() for non-static member functions, reserving self::member() for static member functions.
    Last edited by laserlight; 05-06-2008 at 04:27 AM.
    Use Bazaar for your version control system
    Read the PHP Spellbook
    Learn How To Ask Questions The Smart Way

  3. #3
    Member
    Join Date
    Nov 2007
    Posts
    58
    laserlight, is there any example to show this? i'm still a little confused.

    polymorphism wouldn't work with self::member() ? and i seem to be able to use self::member() for non static ones too.

  4. #4
    PHP Witch laserlight's Avatar
    Join Date
    Apr 2003
    Location
    Singapore
    Posts
    13,907
    laserlight, is there any example to show this? i'm still a little confused.
    Here is an example of correct usage of $this and self for non-static and static member variables:
    PHP Code:
    <?php
    class {
        private 
    $non_static_member 1;
        private static 
    $static_member 2;

        function 
    __construct() {
            echo 
    $this->non_static_member ' '
               
    self::$static_member;
        }
    }

    new 
    X();
    ?>
    Here is an example of incorrect usage of $this and self for non-static and static member variables:
    PHP Code:
    <?php
    class {
        private 
    $non_static_member 1;
        private static 
    $static_member 2;

        function 
    __construct() {
            echo 
    self::$non_static_member ' '
               
    $this->static_member;
        }
    }

    new 
    X();
    ?>
    polymorphism wouldn't work with self::member() ? and i seem to be able to use self::member() for non static ones too.
    Here is an example of polymorphism with $this for member functions:
    PHP Code:
    <?php
    class {
        function 
    foo() {
            echo 
    'X::foo()';
        }

        function 
    bar() {
            
    $this->foo();
        }
    }

    class 
    extends {
        function 
    foo() {
            echo 
    'Y::foo()';
        }
    }

    $x = new Y();
    $x->bar();
    ?>
    Here is an example of suppressing polymorphic behaviour by using self for member functions:
    PHP Code:
    <?php
    class {
        function 
    foo() {
            echo 
    'X::foo()';
        }

        function 
    bar() {
            
    self::foo();
        }
    }

    class 
    extends {
        function 
    foo() {
            echo 
    'Y::foo()';
        }
    }

    $x = new Y();
    $x->bar();
    ?>
    The idea is that $this->foo() calls the foo() member function of whatever is the exact type of the current object. If the object is of type X, it thus calls X::foo(). If the object is of type Y, it calls Y::foo(). But with self::foo(), X::foo() is always called.
    Use Bazaar for your version control system
    Read the PHP Spellbook
    Learn How To Ask Questions The Smart Way

  5. #5
    Member
    Join Date
    Nov 2007
    Posts
    58
    thanks laserlight for those examples, it greatly shows the difference!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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