phpmysqloopobjectuser-object

Clarify a line/s --> setting sql data into object variables


This is my first question here and I will try to clarify it as much as possible.

I am a begginer and I am going through lynda - beyond PHP MySQL lessons when I got to this part.

Code is working just fine, I just need better explanation for myself to the line that is commented in a code.

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 users");
  }

    /////

    public static function find_by_sql($sql="") {

    global $database;
  $result_set = $database->query($sql);
  $user_array = array();
  while ($row = $database->fetch_array($result_set)) {
    $user_array[] = self::instantiate($row);
    }
    return $user_array;
  }

And finaly lines that i almost understand :)

private static function instantiate($row) {
    $user = new self; 

    foreach($row as $attribute=>$value){ 
      if($user->has_attribute($attribute)) { 
        $user->$attribute = $value;          /// THIS LINE BUGS ME
      }
    }
    return $user;

  }

  private function has_attribute($attribute) {

    $user_vars = get_object_vars($this); 
    return array_key_exists($attribute, $user_vars);
  }
}

So I think I don't understand array_key_exists which returns TRUE or FALSE, in my case its true, but then line $users->$attributes =$value ; makes no sense for me,

So, I check if keys from fetch array MATCH variable names from object,

if($user->has_attribute($attribute)) { //and then this is true,perform nxt line
    $user->$attribute = $value;      // i got match of attribute above,how does it put values in $user_vars???

I know it says something like " if user has same attribute as key from that fetch array then put into that same attribute value of that attribute $value but i just dont see how it is done when i never returned object variables

Thank you for your time !

edit: class variables names are equal to names of column_names from database


Solution

  • The User class has public attributes $id, $username, $password, etc. That means that you can assign values to the attributes in the following form:

    $u = new User;
    $u->id = 123;
    $u->username = 'username';
    

    and using dynamic property names:

    $prop = 'id';
    $u->$prop = 123;
    
    $prop = 'username';
    $u->$prop = 'username';
    

    This is just the thing that happens in the lines that you don't understand:

    if ($user->has_attribute($attribute)) {
        $user->$attribute = $value;
    

    The has_attribute method fetches all attributes of the $user object with get_object_vars function. The latter fetches the object properties as an array:

    $user_vars = get_object_vars($this);
    /* i.e.
    $user_vars = array (
      'id' => ...,
      'username' => ...,
      ...
    );
    */
    

    Then the has_attribute method checks if the given $attribute key exists in the array of properties:

    return array_key_exists($attribute, $user_vars);
    

    If it exists (true), the $attribute property is assigned to $value.