Page 1 of 1

Problem with class function

Posted: Wed May 05, 2010 12:48 am
by xionhack
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:

Code: Select all

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;
           }
           }
}

Code: Select all

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;

Code: Select all

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.

Re: Problem with class function

Posted: Wed May 05, 2010 1:01 am
by Weirdan
the problem... its not working.
From my experience php scripts usually either fail with some error message or work (not necessarily in the way you expect them to). Does it throw any errors? What does var_dump($member) show?

Re: Problem with class function

Posted: Wed May 05, 2010 6:24 am
by xionhack
Hi. Thats my biggest problem! it doesnt even show an error! i did print_r($member) and what it shows is the class and its attributes but no values so it was instantiated, but the part of giving the values to the class is not working.

Re: Problem with class function

Posted: Wed May 05, 2010 4:02 pm
by Weirdan
xionhack wrote:but the part of giving the values to the class is not working.
The return statement in the build() method is in the foreach loop - this doesn't seem to be the right place for it. Most probably it should be moved to after the loop.

Re: Problem with class function

Posted: Wed May 05, 2010 4:08 pm
by Weirdan
The following worked for me

Code: Select all

<?php
var_dump(phpversion());

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 $last_name;
}

$_POST['first_name'] = 'first name';
$_POST['last_name'] = 'last name';

$member = Member::build($_POST);
var_dump($member);

giving the following output:

Code: Select all

C:\Windows\system32\cmd.exe /c php-5.3\php.exe q.php
string(5) "5.3.2"
object(Member)#1 (2) {
  ["first_name"]=>
  string(10) "first name"
  ["last_name"]=>
  string(9) "last name"
}
Hit any key to close this window...

Re: Problem with class function

Posted: Thu May 06, 2010 7:04 am
by xionhack
Lol! that was the problem! thanks!