Object to array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
xionhack
Forum Contributor
Posts: 100
Joined: Mon Nov 10, 2008 9:22 pm

Object to array

Post 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!
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Object to array

Post 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.
Last edited by AbraCadaver on Thu Apr 08, 2010 12:43 pm, edited 1 time in total.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
xionhack
Forum Contributor
Posts: 100
Joined: Mon Nov 10, 2008 9:22 pm

Re: Object to array

Post by xionhack »

Sorry, when I call it from another page
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Object to array

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
xionhack
Forum Contributor
Posts: 100
Joined: Mon Nov 10, 2008 9:22 pm

Re: Object to array

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Object to array

Post 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;
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
xionhack
Forum Contributor
Posts: 100
Joined: Mon Nov 10, 2008 9:22 pm

Re: Object to array

Post by xionhack »

Do u have a better way to do all that code that i just did there?!
Post Reply