Call to a member function on a non-object

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
zeek
Forum Commoner
Posts: 48
Joined: Mon Feb 27, 2006 7:41 pm

Call to a member function on a non-object

Post by zeek »

Error: Call to a member function on a non-object

I keep getting the error above on the line marked below (not the actual code, obviously). I have been using an '&' with the database object when making objects. It works until I get to the line marked below as 'problem line'. I don't understand the '&'. Please tell me what I'm doing wrong. Thanks.

My code is like this...

Code: Select all

$db = new Database;

$mod = new Module(&$db);

class Module {

   var $db;

   function Module(&$db) {
      $this->db = &$db;
   }

   function make_objects() {
      $obj = new Object(&$db);
   }

}

class Object {

   var $db;

   function Object(&$db) {
      $this->db = &$db;
   }

   function do_query($query) {
      $this->db->query($query); // problem line
   }

}
User avatar
MrPotatoes
Forum Regular
Posts: 617
Joined: Wed May 24, 2006 6:42 am

Post by MrPotatoes »

put your instatiation afer your declaration. basically the first two snippets of code after your classes

Code: Select all

class Module 
{
   var $db;

   function Module(&$db) { $this->db = &$db; }
   function make_objects() { $obj = new Object(&$db); }
}

class Object 
{
   var $db;

   function Object(&$db) {$this->db = &$db; }
   function do_query($query) { $this->db->query($query);}   // problem line
}

$db = new Database;
$mod = new Module(&$db);
whenever you get that error it means that you are using something that isn't created and isn't PHP therefor it's a class or a function that has yet to exist.

i get it all the time when i shotgun code a new plugin for my framework.
User avatar
zeek
Forum Commoner
Posts: 48
Joined: Mon Feb 27, 2006 7:41 pm

Thank you

Post by zeek »

That was it! I didn't realize that I was doing it out of order like that. Thank you.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Actually that is not the problem. You are passing a local variable to the constructor for Object instead of the "db" property. It should be:

Code: Select all

function make_objects() {
      $obj = new Object($this->db);
      $obj->do_query("SELECT * FROM mytable");
   }
If you are using PHP5 you don't need the ampersands. For PHP4 you have is correct with the &ampersand on the parameter and another ampersand on the assignment.
(#10850)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

I didn't think

Code: Select all

$mod = new Module(&$db);
is valid syntax? (passing by reference dynamically was 'disabled' a long time ago I thought - and also gives warnings now?)
User avatar
zeek
Forum Commoner
Posts: 48
Joined: Mon Feb 27, 2006 7:41 pm

still not working

Post by zeek »

Yea, I got so excited, I posted the reponse before I tested it. It still doesn't work :(

You solved it... $this->db. Thanks.
Post Reply