Okay my goal is to build a Parent with functions to be used in children.

When a child calls a parent class the instance in that parent::method should be made of the child itself and not the parent.

Can you help?

<?php
class test {


protected function set($array = array('1','parent')) {
	$this->id =	$array[0];
	$this->name = $array[1];
}

protected function get() {
	return get_object_vars($this);
}

public function show() {
	$this->set();
return		$this->get();
}

protected function find_by_id() {
	$row = array(array("id" => "igoreee"),array("id" => "max"));
	$object_array= array();
	foreach($row as $record):
		$object_array[] = $this->instantiate($record);	
	endforeach;
	return $object_array;
}

protected function instantiate($record) {
	// Could check that $record exists and is an array					
	foreach ($record as $attribute => $value):
		if ($this->has_attribute($attribute)) {
			$this->$attribute = $value;
		}
	endforeach;
	return $this->attributes();
}
private function has_attribute($attribute) {
	// get_object_vars returns an associative array with all attributes 
	// excluding static vars
	$object_vars = $this->attributes();
	return array_key_exists($attribute, $object_vars);
}
protected function attributes() {				
	// return an array of attribute keys and their values
	return get_object_vars($this);
}
}

class more extends test {
	private $id = '1';
	private $name = '2';

public function set_more() {
	$array = array('2','child');
	parent::set($array);
	return parent::get();
}

public function get_mores_objects() {
	return parent::find_by_id();
}


}

$a = new more;
print_r($a->get_mores_objects());
print '<br />';


?>

if i put private $id in parent I will get back the parents properties but I want to get the child properties instead....

    i dunno if anyone is interested but i solved it here is the result. this is good to build an actionModel for example and then inherit em methods only use what you need and make a new instance of the child for db insertion. use the instance and mail subjects with the same data etc, this is to the fact of no late static bindings and I didnt have time to go figure out how to make it work so just did this so if phpversion is less then 5.3 you can use this. otherwise just go with late static bindings.

    i tested it on 5.2.14 http://aquaregiafx.com/test.php it works tomorrow at work i can test it on 5.2.6 and cross my fingers it works cuz that will make my life so much more simpler.

    <?php
    class test {
    
    public function show(array $array) {
    	$this->set($array);
    	return $this->get();
    }
    
    protected function find(array $row) {
    	$object_array= array();
    	foreach($row as $record):
    		$object_array[] = $this->instantiate($record);	
    	endforeach;
    	return $object_array;
    }
    
    private function instantiate(array $record) {
    	// Could check that $record exists and is an array					
    	foreach ($record as $attribute => $value):
    		if ($this->has_attribute($attribute)) {
    			$this->$attribute = $value;
    		}
    	endforeach;
    	return $this->get();
    }
    
    private function has_attribute($attribute) {
    	// get_object_vars returns an associative array with all attributes 
    	// excluding static vars
    	$object_vars = $this->get();
    	return array_key_exists($attribute, $object_vars);
    }
    
    private function set(array $array) {	
    	foreach($array as $k => $v):
    		$this->$k = $v;
    	endforeach;
    
    }
    
    private function get() {
    	return get_object_vars($this);
    }
    }
    
    class more extends test {
    	protected $id = '1'; // protected for parent 
    	protected $name = '2'; // private from sibling :))
    
    public function set_more() {		
    	$row = array(array("id" => "igoreee"),array("id" => "max"));
    	return parent::find($row);
    }
    
    }
    
    $a = new more;
    print '<pre>';
    print_r($a->set_more());
    print '</pre>';
    print '<br />';
    print '<hr />' . phpversion();
    
    ?>
    

    Hint, if you are to build a table model and the model requires other fields that are in the database table all you have to do is use static keyword for the property to hide it from the get_object_vars() very usefull to store flags or counter.

      Write a Reply...