Jump to content

Problem with class function


xionhack

Recommended Posts

Hello. First of all I have to mention that Im using Late Static Bindings in this application, so the problem that I'm having its not because of the "static" calls. This is my code:

 

 

class DatabaseObject {
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;
}

          public static function build($values_array){
               $object = new static;

              foreach($values_array as $attribute => $value){
             if($object->has_attribute($attribute)){
              $object->$attribute = $value;
             }

              return $object;
           }
           }
}

 

class Member extends DatabaseObject {

protected static $id_name = "member_id";
protected static $table_name="member";
protected static $db_fields = array('member_id' ,'picture' ,'first_name' ,'last_name') 	
                
                
                public $first_name;
                public $first_name;
public $last_name;

 

add_member.php

  <?php $member = Member::build($_POST)?>

<form action="add_member.php" method="post">
First Name: <input type="text" name="first_name" />
Last Name: <input type="text" name="last_name" />
<input type="subtmit" name="last_subtmit" />
</form>

 

Ok, thats an example of what would be my code. If you can see what Im trying is to make the object with the information submitted by the form without having to do a lot of coding... the problem... its not working. I dont see why its not working. Can somebody tell me what is wrong, or tell me how I should go about it? Thanks.

Link to comment
https://forums.phpfreaks.com/topic/200755-problem-with-class-function/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.