好的。我正在設計我的應用程序中的類。設計一個用戶類 - oop php
我有一個用戶類。現在我需要列表中的用戶,獲取有關特定用戶等
起初我試圖做的信息是這樣的:
class User
{
private $userId;
private $name;
private $age;
function __construct()
{
// the code to get the user details from db and assign it to the properties.
}
// get the list of all the users
public function getUserList()
{
}
// add a new user
public function addUser()
{
}
// gets the username
public function getUserName($userId)
{
return $this->name;
}
}
這是我的疑惑:
你認爲從 構造函數中的數據庫填充屬性是一個好主意?如果不是,你可以建議一個更好的方法來做到這一點?
函數getUserList()和addUser()是否真的屬於這個類?或者我應該創建一個單獨的類來獲取用戶列表,添加,刪除用戶,然後另一個類來存儲特定用戶的信息?我應該如何去設計這個?
感謝
您的域對象不應該關心甚至不知道數據庫。您可能想看看Data Mapper模式:http://www.martinfowler.com/eaaCatalog/dataMapper.html – lafor