I'm developing a sort of plugin in Php about posts, comments and likes. So i will have an Object for Post, Comment and Like.
This classes has a bunch of properties that describe the class it self, an small example of the Post
class can be.
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;
}
Now the class called Wall
will be responsible to instantiate and fill the properties of the objects Post
, Comment
, Like
.
I'm injecting the repositories as dependency just for the purpose of this example, on the real class will be injected top classes with the repository as dependency. And even was better to extract this to an interface. This is a ugly class but I want to keep it simple and focus on properties.
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;
}
}
So at this point I know that the properties of those objects should be dynamically but at the same time protected because only 1 class has the responsibility to change it, but to others classes may be useful to get the data of that property.
Ok, well, set getters and setters at this point, but then having 10 properties, I came a cross to have 20 methods more on my class and I want to avoid that as well.
Another way is set magic methods __get
and __set
but it seem the same things that set public properties and maybe is more efficient as well.
Arrived at this point I came with a couple of questions. When i'm talking about properties I'm referring to Post
, Comment
, Like
Objects not the Wall
Class
1) There is any other solution that permit to the class Wall
to edit those properties but still maintain the protected visibility of this properties and not having setters and getters? Will make sense to use reflection?
2) do you think is better to set those properties public? If yes why?
3) When do you determinate when is appropriate to use a specific visibility of the property in a class? (Just want to compare my thought with your)