Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
Thread: PHP OOP - Class extends
07-11-2013, 01:41 PM #1
- Join Date
- Mar 2010
- Thanked 5 Times in 5 Posts
PHP OOP - Class extends
I'm a bit confused about php classes and how extends work.
Say for example I want to make a blog.
So I have a class named blog with the following methods:
blog_insert blog_update blog_delete blog_get_posts blog_view_post
I would then make a new instance of this class to display the blog to the page.
But now I want for example comments in this blog.
So I create a comment class, should this class extend the blog class ore not?
Because I think some properties of blog could come in handy in the comment class.
comment_display comment_insert comment_update comment_delete
07-11-2013, 03:18 PM #2
- Join Date
- Sep 2002
- Saskatoon, Saskatchewan
- Thanked 2,668 Times in 2,637 Posts
Comment should not extend blog as there is no direct relation between the comment and the blog. The comment belongs to the blog, and presumably many comment will belong to blog. So the blog then requires a way of handling comments.
For this, I'd write a blog, and a comment class with no relationship between them. I would then extend blog to a new class which I'll call commentableblog and add methods for comment handling. For ease, I would also add IteratorAggregate to it.
class CommentableBlog extends Blog implements IteratorAggregate
private $comments = array();
public function addComment(IComment $c)
$hash = spl_object_hash($c);
$this->comments[$hash] = $c;
public function removeComment(IComment $c)
public function getIterator()
header('HTTP/1.1 420 Enhance Your Calm');
Users who have thanked Fou-Lu for this post: