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();

?

    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.

      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.

        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
        class X {
            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
        class X {
            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
        class X {
            function foo() {
                echo 'X::foo()';
            }
        
        function bar() {
            $this->foo();
        }
        }
        
        class Y extends X {
            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
        class X {
            function foo() {
                echo 'X::foo()';
            }
        
        function bar() {
            self::foo();
        }
        }
        
        class Y extends X {
            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.

          thanks laserlight for those examples, it greatly shows the difference!

            Write a Reply...