简体   繁体   中英

Best OOP Practices PHP/MySQL

I am creating a word teacher:

I created a database with multiple tables:

TblTheme (themeId, name);<br>
TblGroup (groupId, name, themeIdFK);<br>
TblWeek (weekId, week, groupIdFK);<br>
tblWord (wordId, word, weekIdFK);<br>

So themeIdFK, groupIdFK and weekIdFK are Foreign keys.

In PHP i want to show all the themes. If a theme is pressed, show all the groups etc.. What classes should i create. (like classes Theme, group, week and word) and what is the best way to retrieve the data, without overloading the database?

First of all, if column holds same data, then it should have the same name:

Themes (theme_id, name);
Groups (group_id, name, theme_id);

Then when you create JOIN, you can write it as

SELECT 
   Themes.name AS theme,
   Groups.name AS group
FROM Themes 
LEFT JOIN Groups USING theme_id

As for you original question: I would go with classes:

  • Teacher - that would be your MVC model
  • Word - used by model, holds you daily word and some additional info about it
  • WordMapper - responsible for storing and retrieving instances of Word class

Basically this setup would implement DataMapper pattern .

There is no point in making a class per table, because the additional tables are only holding info about the Words. At lest that's how i see it.

Though, I'm not so sure if you need to make a complex App, just for making word-of-day thing.. maybe only as exercise.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM