Static Variables and self construction

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
styks1987
Forum Newbie
Posts: 8
Joined: Wed Jun 16, 2010 10:03 am

Static Variables and self construction

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Static Variables and self construction

Post 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";

}
Post Reply