Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中访问子类中的抽象类非静态变量_Php_Oop_Inheritance_Abstract Class_Abstract - Fatal编程技术网

在PHP中访问子类中的抽象类非静态变量

在PHP中访问子类中的抽象类非静态变量,php,oop,inheritance,abstract-class,abstract,Php,Oop,Inheritance,Abstract Class,Abstract,我有一个抽象类,它基本上定义了一系列常量、变量、抽象方法和非抽象/正则方法。它的典型结构是这样的: abstract class ClassName{ const CONSTANT_NAME = "test"; protected static $variable_1 = "value"; protected $variable_2 = "value_2"; protected $variable_3 = "value_3" abstract function doSomething(

我有一个抽象类,它基本上定义了一系列常量、变量、抽象方法和非抽象/正则方法。它的典型结构是这样的:

abstract class ClassName{
 const CONSTANT_NAME = "test";
 protected static $variable_1 = "value";
 protected $variable_2 = "value_2";
 protected $variable_3 = "value_3"
 abstract function doSomething();
 protected function doSomethingElse();
}
难题是当我扩展这个类时,需要访问子类中的受保护变量,例如:

public class ChildClassName extends ClassName{

   public function accessParentClassMembers()
   {
    echo parent::$variable_1;  // WORKS FINE
    echo parent::$variable_2; // OBVIOUSLY DOESN'T WORK because it is not a static variable
   }
}

问题是,我如何访问$variable_2,即子类如何访问抽象父类*成员变量*

您有三个错误。下面是一个工作示例。请参阅代码注释

//    |------- public is not allowed for classes in php
//    |
/* public */ class ChildClassName extends ClassName{

       // has to be implemented as it is declared abstract in parent class
       protected function doSomething() {

       }

       public function accessParentClassMembers() {

           // note that the following two lines follow the same terminology as 
           // if the base class where non abstract

           // ok, as $variable_1 is static
           echo parent::$variable_1;

           // use this-> instead of parent:: 
           // for non static instance members
           echo $this->variable_2;
   }
}
进一步注意,这:

protected function doSomethingElse();
在父类中不起作用。这是因为所有非抽象方法都必须有一个主体。所以你有两个选择:

abstract protected function doSomethingElse();


你有三个错误。下面是一个工作示例。请参阅代码注释

//    |------- public is not allowed for classes in php
//    |
/* public */ class ChildClassName extends ClassName{

       // has to be implemented as it is declared abstract in parent class
       protected function doSomething() {

       }

       public function accessParentClassMembers() {

           // note that the following two lines follow the same terminology as 
           // if the base class where non abstract

           // ok, as $variable_1 is static
           echo parent::$variable_1;

           // use this-> instead of parent:: 
           // for non static instance members
           echo $this->variable_2;
   }
}
进一步注意,这:

protected function doSomethingElse();
在父类中不起作用。这是因为所有非抽象方法都必须有一个主体。所以你有两个选择:

abstract protected function doSomethingElse();


天哪!所以很简单…有时我忘记了最基本的东西…可能会变老…谢谢!:wasaware属于“必须实现,因为它在父类中声明为抽象的”,但跳过了它,可能应该更清楚,我要找的是访问$variable_2,感谢不要我的代码注释,对实例成员的访问遵循相同的术语,就像非抽象的基类一样。都一样。可以使用父项::$member或静态::$member访问静态成员。使用$this->member访问的实例成员。我应该多解释一下static:::$member调用与parent::$member调用的区别吗?尽管这并不真正取决于问题。去做吧,如果有什么事情的话,这将是我所知道的与根本不知道的对比:如果你遵循这篇文章,也许它是最好的:。。这个网站有很好的例子。这么快我就找不到更好的例子了。但是您应该了解static::$member,因为它在PHP5.3中提供了新的可能性。它被称为“后期静态绑定”。还可以读到关于被称为“神圣的废话”的文章!所以很简单…有时我忘记了最基本的东西…可能会变老…谢谢!:wasaware属于“必须实现,因为它在父类中声明为抽象的”,但跳过了它,可能应该更清楚,我要找的是访问$variable_2,感谢不要我的代码注释,对实例成员的访问遵循相同的术语,就像非抽象的基类一样。都一样。可以使用父项::$member或静态::$member访问静态成员。使用$this->member访问的实例成员。我应该多解释一下static:::$member调用与parent::$member调用的区别吗?尽管这并不真正取决于问题。去做吧,如果有什么事情的话,这将是我所知道的与根本不知道的对比:如果你遵循这篇文章,也许它是最好的:。。这个网站有很好的例子。这么快我就找不到更好的例子了。但是您应该了解static::$member,因为它在PHP5.3中提供了新的可能性。它被称为“后期静态绑定”。还可以阅读关于get_called_类的内容