Skip to content

Instantly share code, notes, and snippets.

@nicolas-brousse
Last active August 29, 2015 14:13
Show Gist options
  • Save nicolas-brousse/e5e5432b6fda13402486 to your computer and use it in GitHub Desktop.
Save nicolas-brousse/e5e5432b6fda13402486 to your computer and use it in GitHub Desktop.
PHP POO example
<?php
$post = new Post();
$post->setTitle('Awesome title');
$post->setContent('My very awesome article');
$comment = new Comment();
$comment->setAuthor('Alice');
$comment->setText('That\'s a very great article!');
$post->addComment($comment);
<?php
class Comment
{
protected $post;
protected $author;
protected $text;
public function getAuthor()
{
return $this->author;
}
public function setAuthor($author)
{
$this->author = $author;
return $this;
}
public function getText()
{
return $this->text;
}
public function setText($text)
{
$this->text = $text;
return $this;
}
public function getPost()
{
return $this->post;
}
public function setPost(Post $post)
{
$this->post = $post;
return $this;
}
}
<?php
class Post
{
protected $title;
protected $content;
protected $comments;
public function __construct()
{
$this->comments = array();
}
public function getTitle()
{
return $this->title;
}
public function setTitle($title)
{
$this->title = $title;
return $this;
}
public function getContent()
{
return $this->content;
}
public function setContent($content)
{
$this->content = $content;
return $this;
}
public function getComments()
{
return $this->comments;
}
public function addComment(Comment $comment)
{
$comment->setPost($post);
$this->comments[] = $comment;
return $this;
}
public function removeComment(Comment $comment)
{
if(($key = array_search($comment, $this->comments)) !== false) {
unset($this->comments[$key]);
}
return $this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment