Last active
August 29, 2015 14:13
-
-
Save nicolas-brousse/e5e5432b6fda13402486 to your computer and use it in GitHub Desktop.
PHP POO example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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