开发者

An user class, good approach?

I am building an user class that manage the creation, deletion and modification of a generic user. My class should be used in this way:

# creation
user::create($username, $password, $email); // Does not need of 开发者_运维知识库$id

# modification
$u = new user($id);
$u->edit('password', $new_password);

# deletion
$u->delete();

Basically the class contain a static method create() that obliviously does not require the used id as argument. After the creation you can gather user infos and manage the user creating an instance of the class user and set as argument the $id of the user. Is that a good design or should i create something like:

# creation
$users = new genericUserMethod();
$users->create($username, $password, $email);

# modification
$u = new specificUser($id);
$u->edit('password', $new_password);

# deletion
$u->delete();

...Creating 2 different classes. Or is there any other way?


two popular ways to handle this are Active Record and Data mapper. Doctrine 1 used Active record pattern and Doctrine 2 uses Data Mapper. In short:
- with active record you have class that handles both data and persistence
- with Data Mapper you have data class and class that handles persistence

Also there is Data Access Object pattern which can go on top of either of mentioned above.

Your first example looks like active record pattern with unreasonable static shorthand for building record object (why not have multiple constructors or optional id - null for new, integer for existing).

Second example looks like DAO on top of active record and looks more usual.


This could be an approach:

class User {
    private $id;
    private $name;
    //more fields here

    public function __construct($id = null) {
        $this->id = $id;
        if(!is_null($this->id)) {
            $this->load_user_data();
        }
    }

    protected function load_user_data() {
        //select from DB where id = $this->id and populate fields
    }

    public function save() {
        //if $this->id is null insert the user details in DB and populate $this->id with new user's id
        //else update DB with field (optionally check what has changed and update only if necessary)
    }

    public function delete() {
        //delete user if $this->id is not null
    }

    //fields getters and setters here as needed

}

Usage sample:

$mary = new User(); //fresh new user
echo $mary->getId(); //returns null as this user is not inserted.
$mary->setName('mary');
$mary->save(); //insert user with name mary in the DB
echo $mary->getId(); // returns an id as this user is now inserted

$john = new User(2); // we assume there was a user john in DB with id = 2
echo $john->getName(); //echoes 'john' if this was his name in DB

You can even define static methods in the class like getActiveUsers() that returns an array with the active users for example...

Note: This is intended for quite simple needs, in case you require to do dome complex things I would recommend you to use an ORM library as pointed @What is the question


The first one. Maybe you should look at ActiveRecord/ActiveModel for some further inspirations.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜