Php 之切问近思

时常发问,时常解答,高中生就是这么学习的,难道连提出问题都不会了?

self 关键字与 static 关键字的区别

二者都用来调用静态域和静态方法的,但二者有一个细微的差别:

self 关键字的作用域是声明该域或方法的类;static 关键字的作用域是调用该域或方法的类;

  • 示例

      // TestController.php
      <?php
      namespace App\Http\Controllers;
    
      class TestController extends Controller
      {
          protected static $variable = 'Parent';
    
          public function saySelf(): string
          {
              return self::$variable;
          }
    
          public function sayStatic(): string {
              return static::$variable;
          }
      }
    
      // TestSonController.php
      <?php
      namespace App\Http\Controllers;
    
      class TestSonController extends TestController
      {
          protected static $variable = 'Son';
      }
    
  • Tinker 运行

      >>> namespace App\Http\Controllers
      >>> $a = new TestSonController();
      => App\Http\Controllers\TestSonController {#1049}
      >>> $a->sayStatic();
      => "Son"
      >>> $a->saySelf();
      => "Parent"
    
  • 举一反三

      return new static;  // 返回调用类的实例
      return new self;    // 返回声明者的实例
      return new parent;  // 返回调用类上一个父类的实例
    
正在加载今日诗词....

Access Statistics

Table of Contents