Storing a static (class) attribute in a relational database
Suppose you are storing instances of a class in a relational table. How would you go about keeping the static attribute of this class? For example:
class WebSiteUser {
private static $common_homepage_content;
private $username;
private $password_hash;
...
}
Complies with:
CREATE TABLE web_site_users
(
username character varying(100) NOT NULL,
password_hash character varying(40) NOT NULL,
...
Where $common_homepage_content
to go then?
Well, if the variable you want to store is simple static
, I have to assume there will be many users ( WebSiteUser
) with the same $common_homepage_content
. In this case, you have to create a new table in db for this attribute only, because this is a 1-> N relationship.
Since static variables make sense for a class, but not at the instance level, it cannot get into a table with instance variables. The generated table can have multiple characters of the username and corresponding passwords, but including common_homepage_content in each entry will duplicate the data. You can put common_homepage_content in a separate table and reference it from each of your records in the first table.