属性可见性,getter和setter


property visibility, getters and setters PHP

我正在开发一种Php插件关于帖子,评论和喜欢。所以我会有用于发布、评论和点赞的对象。

这个类有一堆描述类本身的属性,Post类的一个小例子可以是。

class Post
{
    protected $infoPost;
    protected $postId;
    protected $content;
    protected $poster_type;
    protected $poster_id;
    protected $likes;
    protected $comments;
    // and more but you got the idea
    public function __construct($postId,
                                $poster_type = null,
                                $poster_id = null,
                                $content = null,
                                $likes = null,
                                $comments = null)
           $this->postId = $postId;
           $this->poster_type = $poster_type;
           $this->poster_id = $poster_id;
           $this->content = $content;
           $this->likes = $likes;
           $this->comments = $comments;
}

现在,称为Wall的类将负责实例化并填充对象Post, Comment, Like的属性。

我将库作为依赖注入只是为了这个例子的目的,对真正的类将被注入顶级类与库作为依赖。最好把它提取到一个界面上。这是一个丑陋的类,但我想让它保持简单,并专注于属性。

class Wall
{
    protected $postRepo;
    protected $commentRepo;
    protected $likeRepo;
    protected $post;
    protected $content;
    protected $likes;
    protected $comments;
    public function __construct(PostRepository $postRepo,
                                CommentRepository $commentRepo,
                                LikeRepository $likeRepo)
    {
           $this->postRepo = $postRepo; 
           $this->commentRepo = $commentRepo;
           $this->likeRepo = $likeRepo;
    }
    // Return Post Object
    public function createPost($postId,$posterType,$posterId)
    {
       $postOrmObject = $this->postRepo->create($postId,$posterType,$posterId);
       $post = new Post($postOrmObject->id,$postOrmObject->posterType,$postOrmObject->posterId);
       $this->post = $post;
       $post->setInfoPost($postOrmObject);
       return $post;
    }
    // Return Content Object
    public function createContent($postId,array $contentInfo)
    {
       $contentOrmObject = $this->contentRepo->create($postId,$content)
       $content = new Content($contentOrmObject->postId,$contentInfo);
       $this->content = $content;
       if ($this->post instanceof Post)
       {
          // here where I change the property
          $this->post->setContent($content);
       }
       return $content;
    }
    public function getPost()
    {
       return $this->post;
    }
}

所以在这一点上,我知道这些对象的属性应该是动态的,但同时也是受保护的,因为只有一个类有责任改变它,但对其他类可能有用,以获得该属性的数据。

这里设置了getter和setter,但有了10个属性,我的类就有了20多个方法我也想避免这个。

另一种方法是设置魔术方法__get__set,但它似乎是相同的东西设置公共属性,可能是更有效的。

到达这里时,我带来了几个问题。当我谈论属性时,我指的是Post, Comment, Like对象,而不是Wall

1)有没有其他的解决方案,允许类Wall编辑这些属性,但仍然保持这些属性的保护可见性,没有setter和getter ?使用反射有意义吗?

2)你认为将这些属性设置为public会更好吗?如果是,为什么?

3)你什么时候决定在类中使用特定的属性可见性是合适的?(只是想和你比较一下我的想法)

1)动态保护(或私有)值的方法是使用数组。protected $data = array();然后,您可以对受保护的值使用__set__get方法,因为您不想使用getter和setter。反射将使您的值可访问,即使它们是受保护的或私有的。

2)我个人主要使用受保护的值,在你的情况下,我也会保持它们受保护。其他对象不能仅仅改变这些值,它们属于那个对象。

3)继续2。对我来说,大多数价值观都是受到保护的。它们属于特定的对象,任何人都不应该改变它们,也不应该显示它们。如果一个不同的对象想要改变或显示值,它应该通过保存数据的当前对象,并且应该告诉调用对象如何以及如何显示。

我个人喜欢这篇文章:https://stackoverflow.com/a/21902271/660410它没有直接回答你的问题,但给了一个很好的理解三个可见性关键字。