tags:

views:

96

answers:

2

Suppose you store instances of a class in a relational table. How would you go about persisting a static attribute of that class? For example:

class WebSiteUser {
    private static $common_homepage_content;
    private $username;
    private $password_hash;
    ...
}

Corresponding to:

CREATE TABLE web_site_users
(
  username character varying(100) NOT NULL,
  password_hash character varying(40) NOT NULL,
  ...

Where does $common_homepage_content go then?

+2  A: 

As static variables make sense for a class but not on the instance level it can't go inside the table that has instance variables. In the table that you are creating there can be multiple username characters and corresponding password_hashes but putting common_homepage_content in each record would be duplication of data. You can put common_homepage_content in a separate table and have a reference to it from each of your records in the first table.

Raminder
+1  A: 

Well, if the variable you want to persist is just static I must assume that there are going to be many users (WebSiteUser) with the same $common_homepage_content. In this case, you should create a new table in the db just for this attribute because it's a 1->N relation.

bruno conde