drnf593779 2016-02-11 12:59
浏览 27
已采纳

PHP - 在null上调用成员函数

I have the following three classes:

class Dom_Form_Section extends Dom {

  /* ... code ommited ... */

  public function addElem($Elem) {
      if (is_a($Elem, 'Dom_Form_Elem')) $FormElem=$Elem;
      else $FormElem=Dom_Form_Elem::create(array(), $Elem);

      if ($FormElem !== false) $this->FormElems[]=$FormElem;

      return $FormElem;
  }
}

class Dom_Form extends Dom {

   private $FormSections=array();

   /* ... code ommited ... */

   public function addElem($Elem) {
    if (is_a($Elem, 'Dom_Form_Elem')) $FormElem=$Elem;
    else $FormElem=Dom_Form_Elem::create(array(), $Elem);

    if ($FormElem !== false) {
        if (empty($this->FormSections)) $Section=$this->addSection();
        else $Section=$this->FormSections[count($this->FormSections)];
        return $Section->addElem($FormElem); // !!! this is where the error fires
    } else return false;
   }

   public function addSection($SectionData=array()) {
    $id=$this->FormId."-section-".count($this->FormSections);

    if (!is_array($SectionData)) $SectionData=array();
    $FormSection=new Dom_Form_Section($SectionData, $id);
    $this->FormSections[]=$FormSection;

    return $FormSection;

 }
}

class Dom_Form_Elem extends Dom {

  public static function create($data, $Elem) {
    if (!is_a($Elem, 'Dom')) return false;
    else {
       $FormElem=new Dom_Form_Elem($data, $Elem);
       return $FormElem;
    }
  }

  /* ... code ommited ... */
}

If I run the following code:

 $Form=new Dom_Form();
 $Form->addElem($Input); // $Input is of 'Dom'

I get the following error:

Fatal error: Call to a member function addElem() on null

If I include some echoes in the two addElem functions (the one in Dom_Form_Section and the one in Dom_Form) they both fire, but the error still persists. It looks to me as if I am making a loop somewhere and that's why I get the error.

Additionally, if I var_dump the contents of the $Section variable, just before the error fires, it is a valid Dom_Form_Section object. The error fires when I try to call the Dom_Form_Section::addElem() method.

What could be wrong with the code?

EDIT:
With the help of @A-2-A I've figured out that the problem was with this line:

else $Section=$this->FormSections[count($this->FormSections)];

I've tried to access an undeclared member of the $this->FormSections array. By changing count($this->FormSections) to count($this->FormSections)-1 the code now works fine.

  • 写回答

1条回答 默认 最新

  • dongzhi4073 2016-02-11 13:40
    关注

    It's not clear what you have in Dom class. So to figure out the problem is bit tough for all of us.

    Please add this code in your file to check all the errors and may be you are able to get the solution yourself.

    <?php
    error_reporting(-1);
    ini_set('display_errors', 1);
    
    ........ rest of your code
    

    Note: please add this code in your file on top just after <?php as I have shown to you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题