PHP 访问父类变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6456939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Accessing Parent Class Variable
提问by Kuntau
class A {
private $aa;
protected $bb = 'parent bb';
function __construct($arg) {
//do something..
}
private function parentmethod($arg2) {
//do something..
}
}
class B extends A {
function __construct($arg) {
parent::__construct($arg);
}
function childfunction() {
echo parent::$bb; //Fatal error: Undefined class constant 'bb'
}
}
$test = new B($some);
$test->childfunction();
Question: How do I display parent variable in child? expected result will echo 'parent bb'
问题:如何在孩子中显示父变量?预期结果将回显“父 bb”
回答by George Cummins
echo $this->bb;
The variable is inherited and is not private, so it is a part of the current object.
该变量是继承的,不是私有的,因此它是当前对象的一部分。
Here is additional information in response to your request for more information about using parent::
:
以下是应您要求提供有关使用的更多信息的附加信息parent::
:
Use parent::
when you want addextra functionality to a method from the parent class. For example, imagine an Airplane
class:
parent::
当您想向父类中的方法添加额外功能时使用。例如,想象一个Airplane
类:
class Airplane {
private $pilot;
public function __construct( $pilot ) {
$this->pilot = $pilot;
}
}
Now suppose we want to create a new type of Airplane that also has a navigator. You can extend the __construct() method to add the new functionality, but still make use of the functionality offered by the parent:
现在假设我们要创建一种也有导航器的新型飞机。您可以扩展 __construct() 方法以添加新功能,但仍可使用父级提供的功能:
class Bomber extends Airplane {
private $navigator;
public function __construct( $pilot, $navigator ) {
$this->navigator = $navigator;
parent::__construct( $pilot ); // Assigns $pilot to $this->pilot
}
}
In this way, you can follow the DRY principleof development but still provide all of the functionality you desire.
回答by Nicola Peluchetti
Just echo it since it's inherited
只是 echo 它因为它是继承的
echo $this->bb;
回答by corretge
With parent::$bb;
you try to retrieve the static constant defined with the value of $bb
.
随着parent::$bb;
您尝试检索使用 的值定义的静态常量$bb
。
Instead, do:
相反,请执行以下操作:
echo $this->bb;
Note:you don't need to call parent::_construct
if B is the only class that calls it. Simply don't declare __construct in B class.
注意:parent::_construct
如果 B 是唯一调用它的类,则不需要调用。简单地不要在 B 类中声明 __construct 。
回答by user2147836
class A {
private $aa;
protected $bb = 'parent bb';
function __construct($arg) {
//do something..
}
private function parentmethod($arg2) {
//do something..
}
}
class B extends A {
function __construct($arg) {
parent::__construct($arg);
}
function childfunction() {
echo parent::$this->bb; //works by M
}
}
$test = new B($some);
$test->childfunction();`
回答by FinalForm
$bb has now become the private member of class B after extending class A where it was protected.
$bb 在扩展类 A 后,现在已经成为类 B 的私有成员。
So you access $bb like it's an attribute of class B.
因此,您可以像访问 B 类的属性一样访问 $bb。
class A {
private $aa;
protected $bb = 'parent bb';
function __construct($arg) {
//do something..
}
private function parentmethod($arg2) {
//do something..
}
}
class B extends A {
function __construct($arg) {
parent::__construct($arg);
}
function childfunction() {
echo $this->bb;
}
}
$test = new B($some);
$test->childfunction();
回答by Hamed Momeni
all the properties and methods of the parent class is inherited in the child class so theoretically you can access them in the child class but beware using the protected
keyword in your class because it throws a fatal error when used in the child class.
as mentioned in php.net
父类的所有属性和方法都是在子类中继承的,因此理论上您可以在子类中访问它们,但要注意protected
在类中使用关键字,因为在子类中使用时会引发致命错误。
如php.net 中所述
The visibility of a property or method can be defined by prefixing the declaration with the keywords public, protected or private. Class members declared public can be accessed everywhere. Members declared protected can be accessed only within the class itself and by inherited and parent classes. Members declared as private may only be accessed by the class that defines the member.
属性或方法的可见性可以通过在声明前加上关键字 public、protected 或 private 来定义。声明为 public 的类成员可以在任何地方访问。声明为 protected 的成员只能在类本身内以及被继承类和父类访问。声明为私有的成员只能由定义该成员的类访问。