如何使用parent::方法访问父类中的Variable


How to access Variable in parent class using parent:: method

我在父类中有受保护的变量UserId。我将在我的子类中扩展变量,如下所示

class Main
{
  protected $UserId          = "151";
  protected static $UserName = "Madhavan";      
  protected function SampleMethod()
  {
    print "This is Sample Method";
  } 
}

class SubMain extends Main
{   
  function __construct()
  {    
    print parent::SampleMethod();
    print "User Id is ".$this->UserId."<br/>";          
    print parent::$UserName;
    print "User Id is ".parent::$UserId."<br/>";            
  }
}

当我使用$this->UserId它的打印很好。但是当我使用Parent::$UserId时,它的显示错误

致命错误:访问未声明的静态属性:Main::$UserName

为什么它没有显示由parent::SampleMethod()访问的函数,因为该函数不是静态的。

作用域解析运算符::有时表现得不明显。当应用于常量或变量时,它总是以静态方式解析。

然而,当应用于函数时,被调用者的执行上下文取决于调用者代码的执行上下文;上下文没有改变。

例如,这可以在没有任何警告的情况下正常工作:

class Test
{
  private $a = 123;
  public function __construct()
  {
    Test::test();
    self::test();
  }
  public function test()
  {
    echo $this->a;
  }
}
new Test();

调用self::test()Test::test()都以非静态方式运行,因为__construct()是非静态调用的,并且您引用的是同一个类。

要引用任何实例变量,例如上例中的$a,需要使用$this->

这是因为函数是可重写的(因此,同名的旧版本共存),而属性则不是(声明只是相互覆盖,不应在子类中重新声明属性)。如果属性不是静态的,则始终使用$this->访问该属性的ONLY实例,如果属性是静态的,那么使用self::访问该实例。(如果一个属性是在多个祖先类中声明的,那么仍然只存在一个数据字段,因此不能引用任何"其他"或"以前的"。)

如果E_STRICT没有激活,你不会得到错误,否则你会得到这样的东西:

严格标准:在…中不应静态调用非静态方法父级::SampleMethod()

您可以使用带有self关键字的self::$UserName属性(定义它的位置)来访问它,而不是使用parent。如果您想在子类中达到它的值(因为覆盖了它),它可以通过final::$UserName(称为后期静态绑定)

访问