Page 1 of 1

Static Variables and self construction

Posted: Tue Jul 06, 2010 5:23 pm
by styks1987
I am using php 5.3. If you are not familiar with late static bindings please be advised this post involves late static bindings.
The functions below will successfully create variables based on the database fields associated with the class.
What I need the construction function to do is to make the variables static when it creates them.
*** Product Class ***

Code: Select all

class Product extends Common {
    protected static $table_name = 'products';
    protected static $db_fields;

    public function __construct(){
       self::construction ();
    }
}
$product = new Product;
*** Common Class ***

Code: Select all

class Common {
    protected function construction () {
    $this->db_fields = self::find_table_fields();
       foreach($this->db_fields as $key => $field){
        $this->$field = "";
       }
    }
}
Thank you for your help.
Mike

Re: Static Variables and self construction

Posted: Tue Jul 06, 2010 11:27 pm
by requinix
1. That doesn't use LSB.
2. The object design is, uh, lacking.

Since you're doing this in the constructor you don't need anything static.

Code: Select all

abstract class Common {

	protected $table_name = "";
	protected $db_fields = array();

	public function __construct() {
		$this->db_fields = self::find_table_fields($this->table_name);
		foreach ($this->db_fields as $field) $this->$field = "";
	}

	protected static function find_table_fields($table_name) {
		// ...
	}

}

class Product extends Common {

	protected $table_name = "products";

}