manishrestha529 Posted July 22, 2012 Share Posted July 22, 2012 user.php file: <?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=null){ global $db; $result_array = self::find_by_sql("SELECT * FROM users WHERE id={$id}"); return !empty($result_array) ? array_shift($result_array) : "result_array empty"; } public static function find_by_sql($sql=""){ global $db; $result = $db->query($sql); $object_array = array(); while ($row = $db->fetch_array($result)){ $object_arary[] = self::instantiate($row); } return $object_array; } 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; } public function full_name(){ if(isset($this->first_name) && isset($this->last_name)){ return $this->first_name." ".$this->last_name; } else { return ""; } } } ?> index.php file: <?php require_once("../includes/database.php"); require_once("../includes/user.php"); $user = User::find_by_id(1); echo $user->full_name(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/266064-php-instantiation-and-statics/ Share on other sites More sharing options...
manishrestha529 Posted July 22, 2012 Author Share Posted July 22, 2012 The above source code is not giving the ARRAY value in result_array Quote Link to comment https://forums.phpfreaks.com/topic/266064-php-instantiation-and-statics/#findComment-1363394 Share on other sites More sharing options...
manishrestha529 Posted July 22, 2012 Author Share Posted July 22, 2012 What a silly mistake of mine.. got it... object_arary instead of object_array ..... Quote Link to comment https://forums.phpfreaks.com/topic/266064-php-instantiation-and-statics/#findComment-1363398 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.