Jump to content

Recommended Posts

Hey Guys. This is post is a continuation for my last thread. I have created a User  Object composition with my database object class. 

 

In the database class I am querying all the fields in the database, and trying to assign the returned array to the attributes in the User Object not the DatabaseObject Object.

 

The challenge that I am facing is assigning those values in the User Object while keeping that method instantiate in the DatabaseObject class (The method instantiates takes the row and assigns it to the attributes in the object)

 

Right now, it only works if I add the attributes in the DatabaseObject Class not the User Class

 

Is there any work around so that  assign them to the User objects attributes

 

Here is my database class and the instantiate method

Class DatabaseObject {

/*
         * find_by_id returns an object array based on the table_name and id that gets passed in
         */
        
    public  function find_by_id($table_name="", $id=""){
         $result_array = $this->find_by_sql("SELECT * FROM {$table_name} WHERE id = {$id}");
         return !empty($result_array) ? array_shift($result_array) : false; }
    
    
         /*
          * find_by_id uses the find_by_sql to query the sql and then instantiate that row
          * into an object array
          */
    public  function find_by_sql($sql = "") {
     global $database;
     $result_set = $database->query($sql);
     $object_array = array(); // Initilize array to store objects
     while($row = $result_set->fetch_assoc()){
         $object_array[] = $this->instantiate($row);
     }
     return $object_array;
     
      }
      
      /*
       * The instantiate function instantiates a row and 
       * assigns the value to the object attributes
       */
      
      private  function instantiate($record) {
          $object = new self; // How do create a user object??

foreach ($record as $attribute=>$value) {
    if($object->has_attribute($attribute)) {
    $object->$attribute = $value;
    } // of if 
}// End of foreach

return $object;
      } // End of instantiate
      
      private function has_attribute($attribute) {
        $object_vars = get_object_vars($this);
        return array_key_exists($attribute,  $object_vars);
      }
 }

Here is the user class that is calling the find_by_id method

class User  {
    
   private $db;
    public $first_name; 


    public function __construct($db) {
        $this->db = $db;
    }
    
 
     public function find_by_id($table_name,$id) {
     return $this->db->find_by_id($table_name,$id);
     }
    

Here is my index file where I am trying to echo out attributes

$user_set = $user->find_by_id("users","1");
echo $user_set->first_name;
Link to comment
https://forums.phpfreaks.com/topic/283071-need-help-with-object-compostion/
Share on other sites

Create some sort of method which can set variables on an object, then in your User code call that method by passing the values from the database and itself as arguments. If you were to create a base "database object model" class and extend User from it, you can make the method protected and only require the array of values.

$data = $this->db->find_by_id($table_name, $id);
$this->set_variables($data); // method defined in the parent class
Keep the "get data" and "set values on an object" functionality separate - don't adapt find_by_id() and such to set the values immediately. Edited by requinix

Hey requinix. Thanks for you reply. I got some questions about your post.

 

1) Create some sort of method which can set variables on an object
How do I do that?

 

2) Then in your User code call that method by passing the values from the database and itself as arguments

What do you mean by this?

 

3) Keep the "get data" and "set values on an object" functionality separate - don't adapt find_by_id() and such to set the values immediately.

I'm sorry I don't understand what you mean by this either...

1.

foreach ($array as $key => $value) {
	$object->$key = $value;
}
2. A separate function where you pass, as arguments to the function, the values you got from the database and the object you want to set those values on.

function set_variables($object, array $data) {
	foreach ($data as $key => $value) {
		$object->$key = $value;
	}
}
3. Based on the whole "make a function where you tell it what data to use", I thought you might ask "well why not just do all that work in the find_by_id() method and save myself another function call?" The problem is separation of concerns: that find_by_id() should be a small method that does only what it actually needs to do, and that is to get an array of data from the database.

 

In this particular situation you want to put that data on an object, but maybe somewhere else you don't want to do that. Maybe you do want just the array. Or maybe you do need to put it in an object but there's special logic you have to follow, or maybe you only want some of the data in the object, or maybe something else.

Hey Requinix. I am not sure if you understood the intent of how I was using trying to use the code..

 

My goal was to create one database class too do all the find methods.

 

And the user class, to use the databaseobject class  to pull data from the users table. I will then be using the database  object class for other classes like a photograph class, to pull info from the photograph table

 

 

The only issue I was having in the database object was the has_attribute method

 

if you look closely under the instantiate method, it had a foreach statement with the following condition

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

 

The function "has_attribute" checked to see if the database object had the those following attributes that find_by_id pulled only in the DatabaseObject class

 

 

It was the get_object_vars that was giving me the problem..

 

$object_vars = get_object_vars($this);
 return array_key_exists($attribute,  $object_vars);

 

get_object_vars was getting the object vars from the DatabaseObject class using the $this variable.  What I was looking for is to get the attributes in the User object not the DatabaseObject,

 

Once I removed the has_attribute function it worked.  But its not really a big deal. I don't see a point to check if the object_vars exists or not.

 

But thank you very much for you help!!!!

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.