英文:
How to get non-static variable from inherited class?
问题
在这种情况下,如何从第二个类访问第一个类中的属性?
class First
{
public $var = '第一个非静态属性';
public function __construct()
{
echo $this->var;
}
}
class Second extends First
{
public $var = '第二个非静态属性';
public function __construct()
{
parent::__construct(); // 调用父类的构造函数以访问属性
}
}
$object = new Second();
我遇到了一个错误:
致命错误:未捕获的错误:访问未声明的静态属性:First::$var 在 /Applications/MAMP/htdocs/tests/index.php 的第 19 行。
英文:
In such a case, how to access the property in the first class from the second class?
class First
{
public $var = 'First Non-Static Property';
public function __construct()
{
echo $this->var;
}
}
class Second extends First
{
public $var = 'Second Non-Static Property';
public function __construct()
{
parent::$var;
}
}
$object = new Second();
Im getting an error;
Fatal error: Uncaught Error: Access to undeclared static property: First::$var in /Applications/MAMP/htdocs/tests/index.php on line 19
答案1
得分: 0
You should brush up on OOP, inheritance and overriding. When you create an instance of a class, the created object contains all of the attributes and methods defined in it, as well as any other methods and attributes of its parent class which it is extending (as long as they have a scope of public and protected).
In your case, the class "Second" does inherit class "First" BUT it does re-initialize the value of your "var" class property that you initially defined in the First class. Therefore, you're basically done with accessing the value that var
has in the first class, because you set it to a new value in the child (Second) class.
If you do, for some reason, want to access it, you can remove the following part in the second class:
public $var = 'Second Non-Static Property';
and then in the second class's constructor, have something like:
public function __construct()
{
echo $this->var;
}
This will print "First Non-Static Property," and then you can redefine the var
property of the Second class as you wish after this call.
The second option that comes to mind is for you to call the parent constructor:
public function __construct()
{
parent::__construct();
}
That will also print out "First Non-Static Property."
Some additional mentions of the difference between parent::
and $this->
can be found here:
https://stackoverflow.com/questions/6456939/php-accessing-parent-class-variable
英文:
You should brush up on OOP, inheritance and overriding. When you create an instance of a class, the created object contains all of the attributes and methods defined in it, as well as any other methods and attributes of it's parent class which it is extending (as long as they have a scope of public and protected)
In your case, the class "Second" does inherit class "First" BUT it does re-initialize the value of your "var" class property that you initially defined in the First class. Therefore, you're basically done with accessing the value that var has in the the first class, because you set it to a new value in the child (Second) class.
If you do for some reason want to access it, you can remove the
public $var = 'Second Non-Static Property';
part in the second class, and then in the second class in the constructor have something like:
public function __construct()
{
echo $this->var;
}
this will print "First Non-Static Property"
and then you can redefine the var property of the Second class as you with after this call.
Second option that comes to mind is you to call the parent constructor
public function __construct()
{
parent::__construct();
}
That will also print out the "First Non-Static Property".
Some additional mentions of the difference between parent:: and $this-> can be found here
https://stackoverflow.com/questions/6456939/php-accessing-parent-class-variable
答案2
得分: -1
你可以像这样访问父类非静态属性:
<?php
class First
{
public $var2 = 'First Non-Static Property';
public function __construct()
{
echo $this->var2;
}
}
class Second extends First
{
public $var = 'Second Non-Static Property';
public function __construct()
{
echo $this->var2;
}
}
$object = new Second();
?>
如果你访问名为var的变量,它将返回子类字符串,因为发生了重载,所以如果你给变量取不同的名称,你可以像这样访问父类的公共和受保护的非静态变量。
希望这对你有帮助。
英文:
You access parent class non-static attributes like this:
<?php
class First
{
public $var2 = 'First Non-Static Property';
public function __construct()
{
echo $this->var2;
}
}
class Second extends First
{
public $var = 'Second Non-Static Property';
public function __construct()
{
echo $this->var2;
}
}
$object = new Second();
?>
If you access variable named var then it will gonna return child class string due to overloading, so if you give different name to variable then you access parent public and protected non-static variables like this.
Hope this is helpful
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论