Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/251.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
正确的实现方式:DAO设计模式php+;pdo_Php - Fatal编程技术网

正确的实现方式:DAO设计模式php+;pdo

正确的实现方式:DAO设计模式php+;pdo,php,Php,例如: class UserMapper { protected $db; public function __construct(PDO $db) { $this->db = $db; } public function save(User $user_object) { } public function getUserById($id) { //code } }

例如:

class UserMapper {

    protected $db;

    public function __construct(PDO $db) {
        $this->db = $db;
    }

    public function save(User $user_object)
    {

    }


    public function getUserById($id)
    {
        //code
    }


}

class User {

    private $id;
    private $username;

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

    public function __set($name, $value)
    {
    }

    public function __get($name)
    {
        return $this->$name;
    }

}

这是实现模式的正确方法吗?

好吧,就这样了,但模式还有更多。您可能想查看这篇文章和参考资料,其中对模式进行了深入的解释

$user = new User();
$user->username = 'New Username';
$user_mapper = new UserMapper($db); //Pdo object = $db
$user_mapper->save($user);