【问题标题】:How do I make a generic DAO interface in PHP?如何在 PHP 中创建通用 DAO 接口?
【发布时间】:2019-09-03 17:02:07
【问题描述】:

我正在尝试用 PHP 编写一个通用的 DAO 接口。我知道它在 Java 中的样子,但我只知道它在 PHP 中的样子。

我已经在 PHP 中尝试过。

<?php
 interface DAO {

    public function create($obj);
    public function read();
    public function update($obj);
    public function delete($obj);
 }

因为我想要这样的 Java 接口

public interface DAO<T> {

    void create(T ob);
    List<T> read();
    void update(T ob);
    void delete(String id);

}

我希望能够像在 PHP 中那样编写接口,但我无法将通用对象添加到接口中。

【问题讨论】:

    标签: java php dao


    【解决方案1】:

    通用 DAO 的最简单形式是在对象级别提供基本的 CRUD 操作,而不暴露持久性机制的内部。

    interface UserDao
    {
        /**
         * Store the new user and assign a unique auto-generated ID.
         */
        function create($user);
    
        /**
         * Return the user with the given auto-generated ID.
         */
        function findById($id);
    
        /**
         * Return the user with the given login ID.
         */
        function findByLogin($login);
    
        /**
         * Update the user's fields.
         */
        function update($user);
    
        /**
         * Delete the user from the database.
         */
        function delete($user);
    }
    

    【讨论】:

      猜你喜欢
      • 2015-10-29
      • 1970-01-01
      • 1970-01-01
      • 2016-04-13
      • 1970-01-01
      • 1970-01-01
      • 2017-10-20
      • 2011-12-19
      • 2012-06-03
      相关资源
      最近更新 更多