spires Posted August 22, 2009 Share Posted August 22, 2009 Hi guys. I'm just learning OOP for the first time, I'm trying to create a user class (see below). But I can't seem to get it to work, I know there's an error, but my server is set up so it doesn't show any error. I would really appreciate any help Class <?PHP // Users Class 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 photo_gallery_users"); } ////// public static function find_by_id($id="") { global $database; $result_array = self::find_by_sql("SELECT * FROM photo_gallery_users WHERE photo_id='{$id}' LIMIT 1"); return !empty($result_array) ? array_shift($result_array) : false; } ////// public static 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; } ////// private static function instantiate($record) { $object = new self; foreach($record as $attribute=>$value) { if($object->has_attribute($attribute)) { $object->$attribute = $value; } } return $object; } ////// private function has_attribute($attribute) { $object_vars = get_object_vars($this); return array_key_exists($attribute, $object_vars); } } ?> HTML <?PHP require_once("../includes/database.php"); require_once("../includes/user.php"); // record or row $user = User::find_by_id(1); echo $user->id; echo "<hr />"; $users = User::find_all(); foreach ($users as $user) { echo "ID: " . $user->id . "<br>"; echo "user: " . $user->username . "<br>"; } Thanks Quote Link to comment Share on other sites More sharing options...
ignace Posted August 22, 2009 Share Posted August 22, 2009 We are not debugging tools. Add this code to the top of your script: error_reporting(E_ALL); ini_set('display_errors', 1); Quote Link to comment Share on other sites More sharing options...
spires Posted August 22, 2009 Author Share Posted August 22, 2009 Hi Thanks I didn't know I could do that to see the errors. Anyway, I fix one of the bugs, but I am left with a new one. Error 1: Notice: Trying to get property of non-object in /var/www/html/businessmobiles.com/OOP/photo_gallery/public/user3.php on line 9 Error 2: Warning: Invalid argument supplied for foreach() in /var/www/html/businessmobiles.com/OOP/photo_gallery/public/user3.php on line 14 $user = User::find_by_id(1); echo $user->id; echo "<hr />"; $users = User::find_all(); foreach($users as $user) { echo "User: ". $user->username ."<br />"; echo "Name: ". $user->first_name ."<br /><br />"; } Error 1 points to : echo $user->id; Error 2 points to : foreach($users as $user) { Any help on this would be great as i'm completely stuck. Thanks Quote Link to comment Share on other sites More sharing options...
ignace Posted August 22, 2009 Share Posted August 22, 2009 Error 1: Notice: Trying to get property of non-object in /var/www/html/businessmobiles.com/OOP/photo_gallery/public/user3.php on line 9 Error 2: Warning: Invalid argument supplied for foreach() in /var/www/html/businessmobiles.com/OOP/photo_gallery/public/user3.php on line 14 The first error points out that $user is not an object. Try var_dump($user); The second error is because of: return = self::find_by_sql("SELECT * FROM photo_gallery_users"); Which should be: return self::find_by_sql("SELECT * FROM photo_gallery_users"); Quote Link to comment Share on other sites More sharing options...
spires Posted August 22, 2009 Author Share Posted August 22, 2009 Hi First, I really appreciate your help on this, I never would have got this far with you. Anyway. var_dump($user); is outputting: array(10) { [0]=> string(1) "1" ["photo_id"]=> string(1) "1" [1]=> string(6) "spires" ["photo_user"]=> string(6) "spires" [2]=> string(7) "ss16" ["photo_pass"]=> string(7) "ss16" [3]=> string(4) "Jeff" ["photo_Fname"]=> string(4) "Jeff" [4]=> string(6) "Spires" ["photo_Lname"]=> string(6) "Spires" } The 2nd error: I have removed the = but I am still getting the same error. Any more ideas would be great. Quote Link to comment Share on other sites More sharing options...
ignace Posted August 22, 2009 Share Posted August 22, 2009 Hi First, I really appreciate your help on this, I never would have got this far with you. Anyway. var_dump($user); is outputting: array(10) { [0]=> string(1) "1" ["photo_id"]=> string(1) "1" [1]=> string(6) "spires" ["photo_user"]=> string(6) "spires" [2]=> string(7) "ss165le" ["photo_pass"]=> string(7) "ss165le" [3]=> string(4) "Jeff" ["photo_Fname"]=> string(4) "Jeff" [4]=> string(6) "Spires" ["photo_Lname"]=> string(6) "Spires" } The 2nd error: I have removed the = but I am still getting the same error. Any more ideas would be great. change $users->id to $users['id'] Second do a var_dump($users); before foreach ($users as $user) Quote Link to comment Share on other sites More sharing options...
spires Posted August 22, 2009 Author Share Posted August 22, 2009 Excellent, first one is working 2nd var dump: resource( of type (mysql result) Quote Link to comment Share on other sites More sharing options...
ignace Posted August 22, 2009 Share Posted August 22, 2009 Excellent, first one is working 2nd var dump: resource( of type (mysql result) Meaning the second is still a resource and is not yet resolved to any data structure like array or object. Quote Link to comment 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.