dtkwt62022 2017-01-18 22:35
浏览 30
已采纳

如何在oop中设计一个有关系(使用php)

I am really new in OOP, and not a good English talker, so if my question is dumb, I am sorry. I thought I understand the oop but when I start codding with it, it works but does not feel right. the following code is my first try, I will ask my question after them. (I cut the database codes, but as I said the program works.)

// class.Post.inc

class Post {
    private $_db;
    private $_postId;
    private $_title;
    private $_creatorId;

    public function __construct() {
        $this->_db = Database::getInstance ()->getConnection ();
    }

    public function getApost($postId = 0) {
        if($postId){
            /* get the post data from database(including user_id) and put them in the instance variables */
            $this->_postId = $row ['post_id'];
            $this->_title = $row ['title'];
            $this->_creatorId = $row ['user_id'];
        }
    }

    public function getCreator() {
        $creator = new User ();
        return $creator->getUserObject ( $this->_creatorId );
    }

    public function getPostId() {
        return $this->_postId;
    }

    public function getTitle() {
        return $this->_title;
    }
}

//class.User.inc

class User {
    private $_db;
    private $_userId;
    private $_name;

    public function __construct() {
        $this->_db = Database::getInstance ()->getConnection ();
    }

    public function getUserObject($userId = null) {
        if ($userId) {
            /* read data from database and put them in the instance variables */
            $this->_userId = $row ['user_id'];
            $this->_name = $row ['name'];

            return $this;
        }

        return false;
    }

    public function getUserId() {
        return $this->_userId;
    }

    public function getName() {
        return $this->_name;
    }
}

//index.php

spl_autoload_register ( function ($className) {
    require_once 'classes/class.' . $className . '.inc';
} );

$post = new Post ();
$post->getApost(1);
$creator = $post -> getCreator();
echo $post->getPostId () . '<br>';
echo $post->getTitle () . '<br>';
echo $creator->getName () . '<br>';

my questions:

  1. post has a creator, do I design this relationship right?
  2. what if I want to use join to select post and its creator together?
  3. if I want to show a list of posts, should I define a new method in the post class or in the index page or define a new class for it? how?
  • 写回答

1条回答 默认 最新

  • doushi2902 2017-01-19 00:37
    关注

    There is much that can be said about your code. I could write what you did wrong few hours, but you didn't asked for that, so I answer your questions firsts.

    1. Yes, it's ok to get creator from post from the perspective of entity relations.
    2. I don't see any clean way using your implementation. It's possible, but it would require major refactoring.
    3. If you want to use Active Record pattern, you could of course implement new method in Post class. But I much more recommend using Data Mapper pattern which is cleaner from OOP perspective because of SRP.

    One piece of advice at the end: I often see new developers to reinvent a wheel, which is exactly what you are doing. There are great and feature complete libraries which saves you lot of time writing your own. For example Doctrine ORM. Or much simplier Nextras ORM. Learning curve may seem high, but I guarantee, it's worth it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?