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 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么