Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - call to member function on null [duplicate]

Tags:

php

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.

like image 910
Adam Baranyai Avatar asked Aug 25 '26 10:08

Adam Baranyai


1 Answers

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.

like image 127
Anant Kumar Singh Avatar answered Aug 27 '26 02:08

Anant Kumar Singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!