3

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.

Adam Baranyai
  • 3,635
  • 3
  • 29
  • 68
  • you get any errors? in php log or display? – Naumov Feb 11 '16 at 13:03
  • 2
    What is in de Dom class? And didn't you get any error on the line new Dom_Form();? – TJVB Feb 11 '16 at 13:03
  • No, the error fires just before calling the `Dom_Form_Section::addElem()` method. The exact error displayed is as follows: *Fatal error: Call to a member function addElem() on null in /location/of/the/Dom_Form/class/file/ on line 57* I've notedin the code where exactly is the error being fired – Adam Baranyai Feb 11 '16 at 13:05
  • 2
    What is there in `Dom`class? add`error_reporting(E_ALL);ini_set('display_errors',1);` on top of your page just after ` – Alive to die - Anant Feb 11 '16 at 13:06
  • Yay, found it thanks @A-2-A. By setting E_ALL, I've noticed that the error is in the `else $Section=$this->FormSections[count($this->FormSections)];`. Undefined offset:) Thanks – Adam Baranyai Feb 11 '16 at 13:08
  • I am adding it as an answer, please mark and up-vote it. thanks – Alive to die - Anant Feb 11 '16 at 13:36
  • @AdamBaranyai i added it as an answer – Alive to die - Anant Feb 11 '16 at 14:07
  • 1
    Given that the question does not contain enough information to diagnose the problem and the OP not giving the solution either after he found the issue, this question is pretty much worthless to any future visitors. – Gordon Feb 11 '16 at 14:18
  • @Gordon I've posted the solution to the original question, the upvote to **A-2-A** answer is because of the guidance which helped me solve the problem. As I don't like answering my own questions only if it is really necessary, I will keep the selected answer as the correct one, until someone cares to post my solution(or another solution if there is one) as an answer. Thanks – Adam Baranyai Feb 11 '16 at 14:25

1 Answers1

3

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.

Gordon
  • 312,688
  • 75
  • 539
  • 559
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98