Applying OOP in small open source project
So my current understanding of classes are: Singleton for a class that will only ever be instantiated once. Static for a class that doesn't get instantiated but just exists. Regular? For a class that can get instantiated over and over.
So I'm doing a small open source project and as for dealing with users, I thought of how I could deal with it, for example: Creating a user - I could instantiate a users object and then call a method create on it. Or I could have a singleton so the users object always exists and call create on that?
I just think it seems sort of sloppy to create an object for each user related action, like updating a users credent开发者_如何学Pythonials, would I want to instantiate another user object and then call a method update on it?
Just confused about how to actually apply OOP, and the best way to do.
Thanks for any/all help you guys can provide.
Even if it's a small project I'd recommend looking at the available PHP frameworks. CodeIgniter leaves a small footprint and embraces fast deployment.
For this case, if we leave out the possible usage of frameworks I'd go with a User class that would look something like this:
class User{
private $user = array();
public function __construct($user_id = 0){
if($user_id !== 0){
$this->user = $this->get($user_id);
}
}
public function get($user_id){
// .. code
}
public function update($data, $user_id = 0){
if($user_id == 0){
$user_id = $this->user['user_id'];
}
// .. code
}
public function create($data){
// .. code
}
public function delete($user_id){
// .. code
}
}
精彩评论