Jump to content
xisto Community
Sign in to follow this  
Ahsaniqbalkmc

Strange Problem In Php Oop On Localhost

Recommended Posts

I am trying to get into the Object Oriented Programming (OOP) in the PHP. For this I have installed a local apache server to test my work and experiment with my scripting techniques. Until today everything was going fine and my learning speed was good. But I have come to face a problem to which I have no answer.

I am trying to create a user class for a test project. The code of the user class is as follows:

<?phprequire_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->passwrod=$record['password'];  $object->first_name=$record['first_name'];  $object->last_name=$record['last_name'];  return $object;}}?>

As you can see in the code, I have created a User class with few attributes and methods. The code is clean and there is no problem in it. On the index page, I have the following code:

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

Theoretically, the code should return the full name of a user but instead when I run the code, 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 8Call Stack# Time Memory Function Location1 0.0014 669408 {main}( ) ..\index.php:0

And I don't understand why I am getting this error. It says that I am making a call to an object function from a non-object. But as per the code in the instantiate method of the user class, the object should get automatically instantiated. I have spend more than an hour just to figure out any mistake in the code but I am unable to find it. So the last guess that is left with me is that there are some settings that I need to change in the php.ini file on my localhost.

Any help from the experts is appreciated.
Thanks in advance

Share this post


Link to post
Share on other sites

As I understand just by looking, the variable $user is not an object, it has the value of the method find_by_id();

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.