×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Call to a member function on a non-object

Call to a member function on a non-object

Call to a member function on a non-object

(OP)
Hi,
I have the following class:

CODE -->

class User
    {  private $_db;
       
       public function __construct($user = null)
       {
          $this->_db = DB::getInstance();
           .......
       }
       public function find($user = null)
       { if($user)
         { $field = (is_numeric($user)) ? 'id' : 'username';
         $data = $this->_db->get('users', array($field, '=', $user));
           if($data->count())
           { .........
             return true;
           }
         }
         return false;
       } 

In class DB I have:

CODE -->

public static function getInstance()
      { if(!(self::$_instance)): self::$_instance = new self(); 
        endif;
        return self::$_instance;
      } 

public function get() in class DB, after processing, returns $this, the single instance of class DB, this is assigned to $data which is then used to invoke public function count(), a member function of class DB. I am getting the following error:

Fatal error: Call to a member function count() on a non-object.

I would be very grateful if someone can point out my mistake.

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

i think you'd need to show us all the code.

the use of static methods and singletons is always a bit tricksy. likewise chaining of static methods.

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

(OP)
Hi,
I have now included all of the relevant code. The code relating to PDO I got off the internet with which I am not fully conversant:
In class User:

CODE -->

public function find($user = null)
   { if($user)        //if username is passed, not null
     { $field = (is_numeric($user)) ? 'id' : 'username';
       $data = $this->_db->get('users', array($field, '=', $user));
       if($data->count())
       { $this->_data = $data->first();
         return true;
       }
     }
     return false;
   } 

In class DB:

CODE -->

class DB
    {
       private static $_instance = null;
       private $_pdo,
               $_query,
               $_error = false,
               $_results,
               $_count = 0;

      private function __construct()
      { try
        { $dsn = sprintf( "mysql:host='';dbname='';", 
                          Config::get('mysql/host'), 
                          Config::get('mysql/database')
                       );
          $this->_pdo = new PDO( $dsn, Config::get('mysql/username'), Config::get('mysql/password'));
        }
        catch(PDOException $excp)
        { die($excp->getMessage());
        }
      }

      public static function getInstance()
      { if(!(self::$_instance)): self::$_instance = new self(); 
        endif;
        return self::$_instance;
      }

       public function get($table, $where)
       { return $this->action('SELECT*', $table, $where);
       }

       public function action($action, $table, $where = array())
       { if(count($where) === 3)    //field, operator, value
         { $operators = array('=', '>', '<', '>=', '<=');

           $field    = $where[0];
           $operator = $where[1];
           $value    = $where[2];

           if(in_array($operator, $operators))
           { $sql = "{$action} FROM {$table} WHERE {$field} {$operator} ?";
             if(!$this->query($sql, array($value))->error())
             { return $this;      //if not an error return $this
             }
           }
         }
         return false;   //outside of everything return false
       }

      public function query($sql, $darams = array())
      { 
        $this->_error = false;
        if($this->_query = $this->_pdo->prepare($sql))
        { $x = 1;
          if(count($darams))
          { foreach($darams as $param)
            { $this->_query->bindValue($x, $param);
              $x++;
            }
          }
          if($this->_query->execute())
          { $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
            $this->_count = $this->_query->rowCount();
          }
          else      //an error has occured in SQL query
          { $this->_error = true;
          }
        }
        return $this;
      } 

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

the action method can return false.

the get method is incorrect.

CODE

public function get($table, $where)
       { return $this->action('SELECT*', $table, $where);
       } 

there should be a space between SELECT and *

this line

CODE

if(!$this->query($sql, array($value))->error()) 
looks most bizarre as there is no error() method of the DB class.

forgetting the chaining for a moment (after all, why is chaining necessary here) perhaps this construct would be rather easier

CODE

$this->query($sql, array($value));
if(isset($this->_error) && $this->_error != ''):
 return false;
else:
 return $this;
endif; 

and then really remember to check for false in your calling code. If you are never going to return false then remember that you must check for the presence of an error before you doing anything else. Hence why chaining is far from sensible on database abstraction layers.

note also that there is no count() method in DB either. so this will always throw a fatal error.

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

(OP)
Hello,
Thanks for the reply. I will study and apply your suggestions. Just wanted to say that there are error and count functions which I forgot to include but I don't think they make too much difference.

CODE -->

public function error()
       { return $this->_error;
       }

       public function count()
       { return $this->_count;
       } 

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

perhaps not. however the point is that the action method can return false. thus the get method can return false

so here, when you get to the conditional, you are asking PHP to invoke a method (count()) on a variable that is not an object but boolean false. So you must always check for boolean false

CODE

$data = $this->_db->get('users', array($field, '=', $user));
if($data !== false && $data->count() > 0):
  $this->_data = $data->first();
  return true;
endif; 

but I say again that you are creating multiple instances of the same object. which I can't believe is sensible. For example here:

CODE

$data = $this->_db->get('users', array($field, '=', $user)); 
at this point you have $data and $this->_db which are identical. why bother chaining? If you're trying to create efficiency of code you'd be better off at creating an inheritable database abstraction class and a single global instance of the $pdo variable.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close