基类函数不显示属性


Base class function not displaying properties

我正在研究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();
?>

我找不到代码中有什么问题。如何解决此问题?

您在setPropertie()方法内部访问属性不正确。从$pr1$pr2属性中删除$以访问它们

错误的

$this->$pr1=$pr1;
$this->$pr2=$pr2;

正确的方式

$this->pr1=$pr1;
$this->pr2=$pr2;