我正在处理PHP继承(刚刚开始使用PHP)。我发现基类方法在使用子类对象访问时不会显示属性值。我的代码看起来像这样。
<?php
class Base
{
public $pr1;
public $pr2;
function __construct()
{
print "In Base class<br>";
}
public function setPropertie($pr1,$pr2)
{
$this->$pr1=$pr1;
$this->$pr2=$pr2;
}
public function display(){
echo "propertie1".$this->pr1."<br>";
echo "propertie2".$this->pr2."<br>";
}
function __destruct()
{
print "Destroying Baseclass<br>";
}
}
class Child extends Base
{
function __construct()
{
parent::__construct();
print "In Subclass<br>";
}
function __destruct()
{
print "Destroying Subclass<br>";
}
}
$obj=new Child();
$obj->setPropertie('Abhijith',22);
$obj->display();
?>我找不到代码中的问题所在。如何解决这个问题?
发布于 2015-10-30 06:07:44
您正在错误地访问setPropertie()方法中的属性。将$从$pr1和$pr2属性中移除以访问它们
错道
$this->$pr1=$pr1;
$this->$pr2=$pr2;正确方式
$this->pr1=$pr1;
$this->pr2=$pr2;https://stackoverflow.com/questions/33429463
复制相似问题