Jump to content

[SOLVED] Object help please - foreach not working.


Recommended Posts

Hi

 

I am currently learning OOP, I am going through a training video but have come across a bug. I would be great if someone could point me in the right direction so I can continue my training.

 

 

$record has been given it's value in another function.

$record: $row = mysql_fetch_array($result_set);

 

 

Now for the bug:

private static function instantiate($record) {

$object = new self();

foreach($record as $attribute=>$value){
if($object->has_attribute($attribute)) {
echo 1;
$object->$attribute = $value;
} else {
echo 2;
}

}
return $object;
}

private function has_attribute($attribute) {
$object_vars = get_object_vars($this);
return array_key_exists($attribute, $object_vars);
}

 

What's going wrong is the foreach loop echo's out 2, which means it is returning false.

 

 

However, this works fine:

private static function instantiate($record) {

$object = new self();

$object->id 		= $record['photo_id'];
$object->username 	= $record['photo_user'];
$object->password 	= $record['photo_pass'];
$object->first_name     = $record['photo_Fname'];
$object->last_name 	= $record['photo_Lname'];

return $object;
}

 

 

Why will it not loop through and place the values inside the object?

I can't see what's wrong, the code is exact to the training videos code.

 

Here is the web page:

http://www.businessmobiles.com/OOP/photo_gallery/public/user3.php

 

As you can see, I'm doing a var dump, but every value is NULL.

 

 

Any pointers would be much appreciated :)

On this line:

if($object->has_attribute($attribute)) {

 

What does has_attribute function do or look like? It looks like you're making an empty object and seeing if it has any attributes, which it won't, hence it returns false.

Hi,

 

has_attribute() is just to check to see if it has an attribute. I'm not to sure why the video have done it this way, but below is the comments that he made about this section.

  // We don't care about the value, we just want to know if the key exists

  // Will return true or false

 

 

If I remove 'if($object->has_attribute($attribute)) {' It still does not return the values.

However, if you check out the var dump now, it is also show an extra array that has one row of the database values showing.

 

Not to sure why that is?

 

var dump:

array(2) { [0]=> object(User)#3 (15) { ["id"]=> NULL ["username"]=> NULL ["password"]=> NULL ["first_name"]=> NULL ["last_name"]=> NULL ["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" } [1]=> object(User)#4 (15) { ["id"]=> NULL ["username"]=> NULL ["password"]=> NULL ["first_name"]=> NULL ["last_name"]=> NULL ["0"]=> string(1) "6" ["photo_id"]=> string(1) "6" ["1"]=> string(7) "livesey" ["photo_user"]=> string(7) "livesey" ["2"]=> string(7) "ss165le" ["photo_pass"]=> string(7) "ss165le" ["3"]=> string(4) "Tina" ["photo_Fname"]=> string(4) "Tina" ["4"]=> string(7) "Livesey" ["photo_Lname"]=> string(7) "Livesey" } }

 

As you can see, they are all NULL till to gets to the end.

 

Any ideas?

 

Thanks

I would guess, that for this to work, the column names in the database would have to be the same as the attribute names in the object.  Your example of something that works shows: $object->id = $record['photo_id']; -- 'photo_id' is the column name in the database but 'id' is the attribute name in the obect; so 'has_attribute('photo_id')' is going to fail.

 

Disclaimer:  I haven't upgraded to PHP5 yet, so I haven't worked with objects in that version.  If there is something special about "attribute" that I don't know, then I could be wrong ... (I thought I was wrong once before, but I was mistaken).

get_object_vars will return null values if the attribute of the object is declared but not defined.  Since you're creating a new version of the container object, but not passing any values to its constructor, those attributes of $object will be null, assuming you didn't assign default values to them in the class definition.

Hi

 

Thanks for all your help. I've posted the full code for the page below.

I think it could be something to with the database name are different to the object names,

but i'm not to sure how to get around that other than changing the database. 

 

Full Code:

<?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=0) {
$result_array = self::find_by_sql("SELECT * FROM photo_gallery_users WHERE photo_id={$id}");
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;
}

public function full_name() {
if(isset($this->first_name) && isset($this->last_name)) {
return $this->first_name . " " . $this->last_name;
} else {
return "";
}
}

private static function instantiate($record) {
// Could check that $record exists and is an array
$object = new self();
// Simple, long-form approach:
/*$object->id 				= $record['photo_id'];
$object->username 	= $record['photo_user'];
$object->password 	= $record['photo_pass'];
$object->first_name = $record['photo_Fname'];
$object->last_name 	= $record['photo_Lname'];*/
//echo $record['photo_id'];
// More dynamic, short-form approach:
foreach($record as $attribute=>$value){

$object->$attribute = $value;

}

return $object;
}

private function has_attribute($attribute) {
// get_object_vars returns an associative array with all attributes 
// (incl. private ones!) as the keys and their current values as the value
$object_vars = get_object_vars($this);
// We don't care about the value, we just want to know if the key exists
// Will return true or false
return array_key_exists($attribute, $object_vars);
}
}


?>

 

 

Any more help would be great, I was getting right in to OOP untill I came across this bug.

 

Thanks :)

Whoo hoo :)

 

I've got it, thanks everyone for all your help, I couldn't have done it without you :)

 

 

Error:

I was using different variable names than the database names.

 

public $photo_id;

public $photo_user;

public $photo_pass;

public $photo_Fname;

public $photo_Lname;

 

 

Thanks once again.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

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.