我有一個名爲「用戶」,用於保存用戶信息的表。我會打電話給我的用戶類並創建一個用戶對象,以便我可以抓取該用戶需要的任何信息。什麼是DB對象交互的其他PHP類設計?
我已經使用下面的類和它們的雜交已經爲我的網站,但一直沒有真正高興與他們主要是因爲我不確定其他開發商如何處理這種情況,如果有更好的解決方案。以下是我一直在使用的一些方法,我很好奇看看其他開發人員在做什麼。
思想#1
class User
{
//public user
public function __construct()
{
//get user - queries table and returns all fields in array
}
//get name function - references user array and returns name
//set name function - sets new name in array
//commit function - commits array to db (saves changes)
}
思想#2
class User
{
//user id
public function __construct(user id)
{
//set user id
}
//get name function - calls db directly and returns name
//set name function - calls db directly and sets name
//commit function - commits array to db saves changes
}
思想#1似乎是DB的呼叫的最有效的,或最少量的但稍微更復雜,比思想不太直接#2。
我喜歡的想法#2更好,但我害怕它的可擴展性,因爲每個功能使得對DB的調用。
思考
看看活動記錄設計模式:http://en.wikipedia.org/wiki/Active_record_pattern – 2011-04-28 18:08:38