dtkwt62022
dtkwt62022
2017-01-18 22:35

如何在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 doushi2902 4年前

    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.

    点赞 评论 复制链接分享

相关推荐