private static function instantiate($record) { $c = new self(); foreach ($record as $attr => $val) { if ($c->has_attribute($attr)) { $c->{$attr} = $val; } } return $c; }
private static function instantiate($record) { $object = new self(); foreach ($record as $attribute => $value) { if ($object->has_attribute($attribute)) { $object->{$attribute} = $value; } } return $object; }
private static function instantiate($record) { // Could check that $record exists and is an array $object = new self(); foreach ($record as $attribute => $value) { if ($object->has_attribute($attribute)) { $object->{$attribute} = $value; } } return $object; }
private static function instantiate($record) { //Could check the $record ecists and is an array $object = new self(); //Simple, long-for approach: // $object->user_id = $record['user_id']; // $object->password = $record['password']; // $object->username = $record['username']; //More dynamic, short-form aapproach: foreach ($record as $attribute => $value) { if ($object->has_attribute($attribute)) { $object->{$attribute} = $value; } } return $object; }
private static function instantiate($record) { // We check that $record exists and is an array with has_attribute // Simple, long-form approach: $object = new self(); // else can use new User // $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 static function instantiate($record) { // could check that $record exists and is an array // this is simple long form approach $object = new self(); // Instantiate an object instance of the User class //$object->userId = $record['userId']; // updating this specific user object with data for this user //$object->username = $record['username']; //$object->hashed_password = $record['hashed_password']; //$object->firstname = $record['firstname']; //$object->lastname = $record['lastname']; // more dynamic short-form approach, using loop - attribute name must be same as db fieldname: foreach ($record as $attribute => $value) { if ($object->has_attribute($attribute)) { $object->{$attribute} = $value; } } return $object; }
private static function instantiate($record) { // Could check that $record exists and is an array $object = new self(); // Simple, long-form approach // $object->id = $record['id']; // $obejct->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; } } }
private static function instantiate($record) { // Could check that $record exists and is an array // create new object (user class) $object = new self(); // 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: // the $record is a row (array) from database, every row has $attribute=>$value (ie. $id=>1) // so we run has_attribute function on $object which checks if the $object has attribute of this name // if true we are assigning $value from $row to this attribute foreach ($record as $attribute => $value) { if ($object->has_attribute($attribute)) { $object->{$attribute} = $value; } } return $object; }
private static function instantiate($record) { $object = new self(); //$object->id = $record["id"]; //$object->uname = $record["uname"]; //$object->fname = $record["fname"]; //$object->lname = $record["lname"]; foreach ($record as $attribute => $value) { if ($object->has_attribute($attribute)) { $object->{$attribute} = $value; } } return $object; }
private static function instantiate($record) { // before we start processing we may want to check if the record exists ??? write it yourself !!! // we could use = new Question(); but because we do it inside the class we may use expression self(); $object = new self(); foreach ($record as $attribute => $value) { if ($object->has_attribute($attribute)) { $object->{$attribute} = $value; } } // end foreach return $object; }