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?