实体类有哪些用途和好处?

时间:2018-05-25 14:22:51

标签: php class oop entity

我经常遇到如下代码(Slim tutorial处的参考github)。

TicketMapper.php

class TicketMapper extends Mapper
{
    public function getTickets() {
        $sql = "SELECT t.id, t.title, t.description, c.component
            from tickets t
            join components c on (c.id = t.component_id)";
        $stmt = $this->db->query($sql);
        $results = [];
        while($row = $stmt->fetch()) {
            $results[] = new TicketEntity($row);
        }
        return $results;
    }
    /**
     * Get one ticket by its ID
     *
     * @param int $ticket_id The ID of the ticket
     * @return TicketEntity  The ticket
     */
    public function getTicketById($ticket_id) {
        $sql = "SELECT t.id, t.title, t.description, c.component
            from tickets t
            join components c on (c.id = t.component_id)
            where t.id = :ticket_id";
        $stmt = $this->db->prepare($sql);
        $result = $stmt->execute(["ticket_id" => $ticket_id]);
        if($result) {
            return new TicketEntity($stmt->fetch());
        }
    }
    public function save(TicketEntity $ticket) {
        $sql = "insert into tickets
            (title, description, component_id) values
            (:title, :description, 
            (select id from components where component = :component))";
        $stmt = $this->db->prepare($sql);
        $result = $stmt->execute([
            "title" => $ticket->getTitle(),
            "description" => $ticket->getDescription(),
            "component" => $ticket->getComponent(),
        ]);
        if(!$result) {
            throw new Exception("could not save record");
        }
    }
}

TicketEntity.php

class TicketEntity
{
    protected $id;
    protected $title;
    protected $description;
    protected $component;
    /**
     * Accept an array of data matching properties of this class
     * and create the class
     *
     * @param array $data The data to use to create
     */
    public function __construct(array $data) {
        // no id if we're creating
        if(isset($data['id'])) {
            $this->id = $data['id'];
        }
        $this->title = $data['title'];
        $this->description = $data['description'];
        $this->component = $data['component'];
    }
    public function getId() {
        return $this->id;
    }
    public function getTitle() {
        return $this->title;
    }
    public function getDescription() {
        return $this->description;
    }
    public function getShortDescription() {
        return substr($this->description, 0, 20);
    }
    public function getComponent() {
        return $this->component;
    }
}

我目前的做法是不使用实体类,我的mapper方法只返回一个stdClass,如下所示:

class TicketMapper extends Mapper
{
    public function getTickets() {
        $sql = "...";
        $stmt = $this->db->query($sql);
        return $stmt->fetchAll(PDO::FETCH_OBJ);
    }
    public function getTicketById($ticket_id) {
        $sql = "...";
        $stmt = $this->db->prepare($sql);
        $result = $stmt->execute(["ticket_id" => $ticket_id]);
        return $stmt->fetch();  //Assuming my PDO is configured to return an object only
    }
    public function save($ticket) {/* no change */}
}

为什么数据库结果经常包含在某个实体类中?是否有任何标准可以决定是否这样做?

2 个答案:

答案 0 :(得分:1)

data mapper的要点是充当业务逻辑和存储之间的持久层。它在实体对象中填充或存储数据。

这些实体有几个目标:

  • 封装:您可以跟踪和控制实体包含的数据的访问和更改方式

  • 验证:确保实体的状态与业务规则和检测能力相匹配,如果实体已进入(或即将进入)无效状态

  • 合同:您可以使用typehints来定义其他模块(类,函数)可以使用此实体的内容以及它可以接受的参数类型。您还可以选择定义该实体的替代品必须实现的接口

  • 行为:一个实体通常会有相关的行为(或业务逻辑),例如:$article->markAsApproved()操作不是一个简单的设置器,而是进行状态的原子更改。

至于标准......好吧......“你是否使用OOP”将是最接近的一个。 stdClass只是一个没有行为的美化数组。

你可能应该看this lecture

答案 1 :(得分:0)

封装:类是一个有用的数据包,包含代码和相关数据,与其他所有内容隔离。这使得在不搜索精确变量的情况下更容易移动它,而不会与现有代码/数据发生冲突。

当然类有其他用途,但在像PHP这样的脚本环境中,我认为最大的优点是。

代码重用

继承

易于维护

等。做一些研究是一件有趣的事情要学习