Page 1 of 1
Object to array
Posted: Thu Apr 08, 2010 10:25 am
by xionhack
Hello. I have a problem, when I instantiate an object from inside the class and then pass it to any page, then it appears as an array, and for me to echo it i have to do something like $array[0][name]->whatever, and Its really annoying. Im very new at object oriented programming and I think there is something Im missing, any help? Thanks!
Re: Object to array
Posted: Thu Apr 08, 2010 11:42 am
by AbraCadaver
What do you mean pass it to any page? GET, POST, SESSION? Those are arrays, so if you store an object there it will be an object in the array.
Re: Object to array
Posted: Thu Apr 08, 2010 11:52 am
by xionhack
Sorry, when I call it from another page
Re: Object to array
Posted: Thu Apr 08, 2010 12:25 pm
by AbraCadaver
xionhack wrote:Sorry, when I call it from another page
You'll have to give a real code example because this doesn't make any sense.
Re: Object to array
Posted: Thu Apr 08, 2010 1:14 pm
by xionhack
Code: Select all
class DatabaseObject {
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[] = static::instantiate($row);
}
return $object_array;
}
protected static function instantiate($record) {
// Could check that $record exists and is an array
$object = new static;
// Simple, long-form approach:
// $object->id = $record['id'];
// $object->username = $record['username'];
// $object->password = $record['password'];
// $object->first_name = $record['first_name'];
// $object->last_name = $record['last_name'];
// More dynamic, short-form approach:
foreach($record as $attribute=>$value){
if($object->has_attribute($attribute)) {
$object->$attribute = $value;
}
}
return $object;
}
private function has_attribute($attribute) {
// 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, $this->attributes());
}
protected function attributes() {
// return an array of attribute names and their values
$attributes = array();
foreach(static::$db_fields as $field) {
if(property_exists($this, $field)) {
$attributes[$field] = $this->$field;
}
}
return $attributes;
}
Code: Select all
class Schedule extends DatabaseObject {
protected static $id_name = "schedule_id";
protected static $table_name="scheduled_studies";
protected static $db_fields = array('schedule_id', 'student_id', 'teacher_id', 'lesson', 'date' , 'time');
public $schedule_id;
public $student_id;
public $teacher_id;
public $date;
public $time;
public $lesson;
public static function find_last_study($student_id) {
$sql = "SELECT * FROM " . self::$table_name . " WHERE stuent_id= {$stuent_id}
ORDER BY date, time DESC";
$last_study = parent::find_by_sql($sql);
return $last_study;
}
Then If I do this
Code: Select all
<?php echo $last_study = Schedule::find_last_study(1); ?>
It will return an array with the object in it, not just the object. Let me know if it makes sense.
Re: Object to array
Posted: Thu Apr 08, 2010 1:26 pm
by AbraCadaver
Well yes. In your find_by_sql() you do this:
Code: Select all
while ($row = $database->fetch_array($result_set)) {
$object_array[] = static::instantiate($row);
}
You are building the array.
If you want to loop through them then:
Code: Select all
$last_study = Schedule::find_last_study(1);
foreach($last_study as $row_object) {
echo $row_object->whatever;
}
Re: Object to array
Posted: Thu Apr 08, 2010 2:15 pm
by xionhack
Do u have a better way to do all that code that i just did there?!