When to use self over $this?

前端 未结 23 2803
醉梦人生
醉梦人生 2020-11-21 11:19

In PHP 5, what is the difference between using self and $this?

When is each appropriate?

相关标签:
23条回答
  • 2020-11-21 12:13

    Here is a small benchmark (7.2.24 on repl.it):

                Speed (in seconds)  Percentage
    $this->     0.91760206222534    100
    self::      1.0047659873962     109.49909865716
    static::    0.98066782951355    106.87288857386
    

    Results for 4 000 000 runs. Conclusion: it doesn't matter. And here is the code I used:

    <?php
    
    class Foo
    {
      public function calling_this() { $this->called(); }
      public function calling_self() { self::called(); }
      public function calling_static() { static::called(); }
      public static function called() {}
    }
    
    $foo = new Foo();
    $n = 4000000;
    $times = [];
    
    // warmup
    for ($i = 0; $i < $n; $i++) { $foo->calling_this(); }
    for ($i = 0; $i < $n; $i++) { $foo->calling_self(); }
    for ($i = 0; $i < $n; $i++) { $foo->calling_static(); }
    
    $start = microtime(true);
    for ($i = 0; $i < $n; $i++) { $foo->calling_this(); }
    $times["this"] = microtime(true)-$start;
    
    $start = microtime(true);
    for ($i = 0; $i < $n; $i++) { $foo->calling_self(); }
    $times["self"] = microtime(true)-$start;
    
    $start = microtime(true);
    for ($i = 0; $i < $n; $i++) { $foo->calling_static(); }
    $times["static"] = microtime(true)-$start;
    
    $min = min($times);
    echo $times["this"] . "\t" . ($times["this"] / $min)*100 . "\n";
    echo $times["self"] . "\t" . ($times["self"] / $min)*100 . "\n";
    echo $times["static"] . "\t" . ($times["static"] / $min)*100 . "\n";
    
    0 讨论(0)
  • 2020-11-21 12:14

    self:: keyword used for the current class and basically it is used to access static members, methods, and constants. But in case of $this you cannot call the static member, method and functions.

    You can use the self:: keyword in another class and access the static members, method and constants. When it will be extends from parent class and same in case of $this keyword. You can access the non static members, method and function in another class when it will be extends from parent class.

    The code given below is a example of self:: and $this keyword. Just copy and paste the code in your code file and see the output.

    class cars{
        var $doors=4;   
        static $car_wheel=4;
    
      public function car_features(){
        echo $this->doors." Doors <br>";
        echo self::$car_wheel." Wheels <br>"; 
      }
    }
    
    class spec extends cars{
        function car_spec(){
            print(self::$car_wheel." Doors <br>");
            print($this->doors." Wheels <br>");
        }
    }
    
    /********Parent class output*********/
    
    $car = new cars;
    print_r($car->car_features());
    
    echo "------------------------<br>";
    
    /********Extend class from another class output**********/
    
    
    $car_spec_show=new spec;
    
    print($car_spec_show->car_spec());
    
    0 讨论(0)
  • 2020-11-21 12:15

    According to http://www.php.net/manual/en/language.oop5.static.php there is no $self. There is only $this, for referring to the current instance of the class (the object), and self, which can be used to refer to static members of a class. The difference between an object instance and a class comes into play here.

    0 讨论(0)
  • 2020-11-21 12:16

    From this blog post:

    • self refers to the current class
    • self can be used to call static functions and reference static member variables
    • self can be used inside static functions
    • self can also turn off polymorphic behavior by bypassing the vtable
    • $this refers to the current object
    • $this can be used to call static functions
    • $this should not be used to call static member variables. Use self instead.
    • $this can not be used inside static functions
    0 讨论(0)
  • 2020-11-21 12:17

    Short Answer

    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.

    Full Answer

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

    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.

    From http://www.phpbuilder.com/board/showthread.php?t=10354489:

    By http://board.phpbuilder.com/member.php?145249-laserlight

    0 讨论(0)
  • 2020-11-21 12:19

    $this refers to the current class object, self refers to the current class (Not object). The class is the blueprint of the object. So you define a class, but you construct objects.

    So in other words, use self for static and this for none-static members or methods.

    also in child/parent scenario self / parent is mostly used to identified child and parent class members and methods.

    0 讨论(0)
提交回复
热议问题