0

I have created a user class with the following code:

  <?php
    require_once("database.php");
    class User {
        public $id;
        public $username;
        public $password;
        public $first_name;
        public $last_name;

        public static function find_all() {
            return self::find_by_sql("select * from users");
        }
        public static function find_by_id($id=0) {
            global $database;
            $result_array=self::find_by_sql("Select * from users where id={$id} limit 1");
            if (!empty($result_array)) {
                return array_shift($result_array);
            } else {
                return FALSE;
            }
        }
        public function find_by_sql($sql="") {
            global $database;
            $result_set=$database->query($sql);
            $object_array=array();
            while ($row=$database->fetch_array($result_set)) {
                $object_array[]=self::instantiate($row);
            }
                return $object_array;
        }
        public function full_name() {
            if (isset($this->first_name)&& isset($this->last_name)) {
        return $this->first_name." ".$this->lastname;
            } else {
                return "";
            }   
        }
        private static function instantiate($record) {
            $object=new self;
            $object->id=$record['id'];
            $object->username=$record['username'];
            $object->password=$record['password'];
            $object->first_name=$record['first_name'];
            $object->last_name=$record['last_name'];
            return $object; 
        }
    }
    ?>

When I try to execute the following code in the index.php

<?php
$user=User::find_by_id(1);
echo $user->full_name();
?>

I get the following error:

( ! ) Fatal error: Call to a member function full_name() on a non-object in C:\wamp\www\imagepro\public\index.php on line 8
Call Stack
#   Time    Memory  Function    Location
1   0.0010  669408  {main}( )   ..\index.php:0

I don't understand why I am getting this error because I think the object has been instantiated and the error says that I am calling an object function from non-object.

Can there be a problem with my PHP settings as I am running this on a localhost. Can anyone help?

tereško
  • 58,060
  • 25
  • 98
  • 150

2 Answers2

5

It means that $user is not an object. Since find_by_id() can return FALSE (which is not an object), my guess is that's what's happening: There simply is no user with ID 1, so you get nothing (FALSE in this case), and you can't get object properties from FALSE.

To solve this, add some kind of check (is_object($user) or $user !== FALSE) before attempting to use it as an object.

tdammers
  • 20,353
  • 1
  • 39
  • 56
1
if (!empty($result_array)) {
                return array_shift($result_array);
            } else {
                return FALSE;
            }

means that $user can be either array or false - this all is not an object.

k102
  • 7,861
  • 7
  • 49
  • 69
  • That's what I though as well, but it seems the OP is calling a lot of static function to generate an array of objects so the return value can be an object. – jeroen Feb 24 '12 at 12:45