I have the following method

function getSectionNumber() {
    return $this->section->getSectionID();
}

which should return the section number of the section class object stores in the section variable but its giving the following error

Fatal error: Call to a member function getSectionID() on a non-object in home/smdbs/new.wgcscouts.co.uk/includes/classes/user.php on line 169

Now i can understand the problem if the section variable didn't contain a section class object but it does, as i can do this

function getSectionNumber() {
    print "Section Num: " . $this->section->getSectionID();
    print "<br/>Class: " . get_class($this->section) . "<br/>";
    var_dump($this->section);

print "<br/><br/><strong>Methods</strong>";
$class_methods = get_class_methods($this->section);

foreach ($class_methods as $method_name) {
    echo "$method_name\n<br/>";
}
return $this->section->getSectionID();
}

which gives me

Section Num: 6
Class: section
object(section)#23 (2) { ["sectionID":"section":private]=> int(6) ["name":"section":private]=> string(7) "Leaders" } 

Methods__construct 
getSectionID 
getName 

Fatal error: Call to a member function getSectionID() on a non-object in /home/smdbs/new.wgcscouts.co.uk/includes/classes/user.php on line 169

    The only thing I can think of is that perhaps your code might inadvertently set $this->section to some non-object just before calling getSectionID() on it. This can happen if you do something like this:

    if ($this->section = NULL) { // note there's only one equal sign. This sets section to NULL rather than checking it.
    }
    

    This assumes, of course, that the code you posted is some stripped down version of your actual code. It's sort of hard to offer any additional help without seeing your actual class.

      ok, i have a members.php which calls the function

      $userManager->getUsersInSectionLinked(1),

      The user manager is created in a config class

      require_once('includes/classes/permissionManager.php');
      $permissionManager = new permissionManager($db);
      
      require_once('includes/classes/userManager.php');
      $userManager = new userManager($db, $permissionManager);

      The user manager is as follows
      http://pastebin.com/MzbUhS4j

      The user is as follows
      http://pastebin.com/vt1XFcWs

      and the section is as follows
      http://pastebin.com/XrKgtbfW

        Line 45 in your user manager:

        $user->setSection($sm->getSectionByID($user->getSectionNo()));

        The function user::setSection takes whatever parameter that gets passed in and assigns it to the internal, private user::$section variable. If you are not passing in an object, then your $user's section will not be an object which means that this line in the user class will throw the error you are talking about:

        function getSectionName() { return $this->section->getName(); }

        Are you certain that $sm->getSectionByID($user->getSectionNo()) is returning an object? You might modify your user::setSection function as follows:

        function setSection($section) {
          if (!($section instanceof section)) {
            throw new Exception("setSection must be called with a section object");
          }
          $this->section = $section;
        }
        

          The function

          function setupSections() {
          		foreach($this->usersList as $user) {
          			$sm = new sectionManager($this->db);
          			$section = $sm->getSectionByID($user->getSectionNo());
          
          		var_dump($section);
          
          		$user->setSection($section);
          	}
          }

          gives the value of

          object(section)#23 (2) { ["sectionID":"section":private]=> int(6) ["name":"section":private]=> string(7) "Leaders" } NULL

          Using the code you posted it says its not a section object

            Are you certain it's a section object every single time it runs? You are running loops here and if any single one is NOT a section object then you are going to have a problem.

              as far as i can tell, when i dump the object in the method causing the error it showing as a section object

              function getSectionNumber() { 
              		var_dump($this->section); 
              		return $this->section->getSectionID();
              	}

              gives

              object(section)#23 (2) { ["sectionID":"section":private]=> int(6) ["name":"section":private]=> string(7) "Leaders" } NULL
              Fatal error: Call to a member function getSectionID() on a non-object in /home/smdbs/new.wgcscouts.co.uk/includes/classes/user.php on line 138

              line 138 being the return

              Now when i do this in the function

              echo "ID: " . $this->section->getSectionID() . " -- Name: " . $this->section->getName();

              ;

              the correct info is being printed

              ID: 6 -- Name: Leaders

              but it still throws the getdectionid error

                Do you see that NULL in your var dump? It's easy to miss. That looks to me like section is NULL for the iteration of your loop right before you get the error.
                Maybe try this for a more informative output:

                function getSectionNumber() { 
                        echo "==== var dumping section ====" . PHP_EOL;
                        var_dump($this->section); 
                        echo "==== end of section var_dump ====" . PHP_EOL;
                
                    return $this->section->getSectionID();
                }
                

                  ok, so the thing i need to work out is why its called twice

                  ==== var dumping section ==== object(section)#23 (2) { ["sectionID":"section":private]=> int(6) ["name":"section":private]=> string(7) "Leaders" }
                  ==== end of section var_dump ====
                  ==== var dumping section ==== NULL
                  ==== end of section var_dump ====

                    Not exactly. It's called twice because of all the loops you have. The userManager constructor calls setupSections which loops through all of your users.

                            function setupSections() {
                                    foreach($this->usersList as $user) {
                                            $sm = new sectionManager($this->db);
                                            $user->setSection($sm->getSectionByID($user->getSectionNo()));
                                    }
                            }
                    

                    So if you have N users, $user->setSection is going to be called N times. What you need to figure out is why a NULL value is being passed into $user->setSection() such that when you later call getSectionNumber then the section has a NULL value.

                    My guess is that your sectionManager is getting a NULL value back from getSectionByID for some user. This is probably caused by what's in your database.

                    I would say that generally speaking your code needs more value checking instead of just assuming the values it passes around are simply going to work -- especially if you are drawing data from a database. If someone deletes a few records, you don't want your code to stop working like it does now.

                      Just a side note, if you post things like var_dump() output within [noparse]

                      ...

                      tags instead of

                      ...

                      [/noparse] tags, it will be easier for us to read (including not interpreting certain character combos as smilies).

                        Write a Reply...