0

这是我创建数据元素列表的代码!在下面提到的这行中有两个错误(在同一行)

1)注意:未定义的偏移量:0 2)致命错误:在非对象上调用成员函数 display()

  <?php
  class data
  {
     public $num;
     public $Char;


    function __construct()
    {
      $this->num= "null";
      $this->Char= "New Char";    
    }

    public function setInt($int)
    {
      $this->num=$int;
    }

    public function setChar($char)
    {
      $this->Char= $char;
    }


    public function getInt()
    {
      return $this->num;
    }

    public function getChar()
    {
     return $this->Char;
    }

    public function display()
    {
      echo $this->num;
      echo $this->Char;
    }
  }

  class linklist extends data 
  {
    public $DATA;
    private $list;
    private $count;

    function __construct()
    {
      $DATA= new data();
      $list= array();
      $count=0;
    }

    function addData(data $d)
    {
       $this->list[$this->count]= $d;

     $this-> count++;
    }   

    function displayy()
    {
        $d= new data();
        $i=0;
      for($i;$i<=$this->count; $i++)
      {
        $this->list[$i]->display();   //** line with error *** //
      }

    }
  }




?>




<!DOCTYPE html>
<html>
<body>
<?php
   $d= new data();

     //$d->display();

     //$Name= $_POST['fname'];
     //$Age = $_POST['age']; 


     $d->setInt("1");    
     $d->setChar("Ashad");   

     //$d->display();

     $d1= new Data();

     $d1->setInt("2");   
     $d1->setChar("shahrukh");   

     $list = new linklist();

     $list-> addData($d);
     $list->addData($d1);


    $list->displayy();


?>  
</body>
</html>
4

1 回答 1

1

的构造函数linklist应该是:

function __construct()
{
  $this->DATA= new data();
  $this->list= array();
  $this->count=0;
}

你已经错过了$this

另请注意@NB 的评论:

for($i; $i <= $this->count; $i++)

应该

for($i = 0; $i < $this->count; $i++)
于 2013-06-02T22:05:14.397 回答