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?
+1
source to share
2 answers
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.
+2
source to share