gaza165 Posted February 2, 2010 Share Posted February 2, 2010 I am doing OOP Programming for my website.... I have created a class called 'Blog' im wondering if it is a better idea, for the comments to create a new COmment class that extends blog to bring back the comments to the related blog or just haev the function inside the Blog class. Quote Link to comment https://forums.phpfreaks.com/topic/190701-need-very-quick-advice/ Share on other sites More sharing options...
Mchl Posted February 2, 2010 Share Posted February 2, 2010 Neither probably. Comment can hardly be described as something that inherits from blog. If anything, blog contains comments. Perhaps your Blog class should contain a commentStore field that would be an array (or ArrayObject, or even SplObjectStorage) storing objects of Comment type. Quote Link to comment https://forums.phpfreaks.com/topic/190701-need-very-quick-advice/#findComment-1005684 Share on other sites More sharing options...
gaza165 Posted February 2, 2010 Author Share Posted February 2, 2010 <?php include("database.php"); class Blog extends DBConnect { private $row; public $title; public $body; public $id; function __construct() { include("bbcode/nbbc.php"); include("bbcode/bbcode_rules.php"); $this->connect(); } function printBlogByTitle($title) { $this->title = str_replace("-"," ",$title); $this->query("SELECT * FROM blog WHERE blog_title = '$this->title'"); while($this->row = $this->fetch()) { echo $this->row['blog_title']; echo $this->bbcode->Parse($this->row['home_body']); $this->GetComments(); } } function getBlogIDByTitle() { $this->query("SELECT blog_id FROM blog WHERE blog_title = '$this->title'"); $this->row = $this->fetch(); return $this->row['blog_id']; } function GetComments() { $this->id = $this->getBlogIDByTitle(); $this->query("SELECT * FROM blog_comments WHERE `blog_id` = $this->id; "); echo "<div class='blogwrapper'>"; while($this->row = $this->fetch()) { echo "<h1 class='title'><a href='/".str_replace(" ","-",$this->row['name'])."/'>".$this->row['name']."</a></h1>"; } echo "</div>"; } function printAllBlogs() { $this->query("SELECT * FROM blog"); echo "<div class='blogwrapper'>"; while($this->row = $this->fetch()) { echo "<h1 class='title'><a href='/".str_replace(" ","-",$this->row['blog_title'])."/'>".$this->row['blog_title']."</a></h1>"; echo "<div class='posted'>"; echo "<span>Posted By <b>".$this->row['posted_by']."</b> on<b> ".date('l jS \of F Y h:i:s A',strtotime($this->row['blog_created']))."</b></span>"; echo "</div>"; echo $this->bbcode->Parse($this->row['home_body']); } echo "</div>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/190701-need-very-quick-advice/#findComment-1005686 Share on other sites More sharing options...
akitchin Posted February 2, 2010 Share Posted February 2, 2010 ... is there a question to go along with your code? Quote Link to comment https://forums.phpfreaks.com/topic/190701-need-very-quick-advice/#findComment-1005687 Share on other sites More sharing options...
gaza165 Posted February 2, 2010 Author Share Posted February 2, 2010 Sorry... my questions is Ive only started OOP, from what you can see from the code, is there anywhere I have gone wrong or need to change?? Cheers Quote Link to comment https://forums.phpfreaks.com/topic/190701-need-very-quick-advice/#findComment-1005689 Share on other sites More sharing options...
gizmola Posted February 2, 2010 Share Posted February 2, 2010 Sorry... my questions is Ive only started OOP, from what you can see from the code, is there anywhere I have gone wrong or need to change?? Cheers It's all opinion. I would not do many of the things you're doing in your class, but that doesn't mean your code won't work. Off the top of my head, my first questions for you: You have the following class variables: public $title; public $body; public $id; Yet you do not setup any of these variable in your constructor. Why not? Also you have one private variable: private $row; Yet it appears from the code that this variable is really only used as a temporary variable -- if so there's no reason to have it in the class. Picking the right class variables is very important to establishing the identity of the class, and its relationship to other classes. As far as phpfreaks is concerned however, you got a really good answer from mchl and you completely seemed to have ignored his advice. Why would we want to delve into the details of what you're doing, when so far you've indicated that you'll just ignore it? Quote Link to comment https://forums.phpfreaks.com/topic/190701-need-very-quick-advice/#findComment-1005700 Share on other sites More sharing options...
gaza165 Posted February 2, 2010 Author Share Posted February 2, 2010 Sorry... I did take what Mchl said and have taken his advice... sorry for not providing any recognition.. :-\ In terms of my class variables, I was under the impression that any variables that use inside the class should be declared at the beginning?? Are you saying that none of the variables I am using are needed to be defined, I can just use normal variables without declaring them? Thanks for everyones input. Garry Quote Link to comment https://forums.phpfreaks.com/topic/190701-need-very-quick-advice/#findComment-1005709 Share on other sites More sharing options...
Mchl Posted February 2, 2010 Share Posted February 2, 2010 You should declare variables that are specific to object instantiating given class. If the variable is used to store temporary results within a method, you don't have to declare it as class variable, rather you should use it as 'normal' local variable (i.e. without $this->) Looking at your class... it doesn't really represent a blog... it seems more like a set of functions to access and display blog-related data. While it might be good start to OOP (to learn syntax and basics concepts mostly), be aware that it doesn't really makes the best use of what OOP can offer you. I would for example split displaying data and retrieving it from database into two different classes. Also perhaps some blog specific behaviour could be abstracted to yet another class... This way we're approaching to Model - View - Controller pattern, which is at heart of most OO software frameworks. Quote Link to comment https://forums.phpfreaks.com/topic/190701-need-very-quick-advice/#findComment-1005715 Share on other sites More sharing options...
gaza165 Posted February 2, 2010 Author Share Posted February 2, 2010 Thanks Mchl Do you think I should investigate MVC frameworks to learn OOP. I have just bought the book PHP,Objects, Patterns and Practices... is this a good book to have bought?? Shall i look into MVC?? What about the Zend Framework? Thanks Garry Quote Link to comment https://forums.phpfreaks.com/topic/190701-need-very-quick-advice/#findComment-1005721 Share on other sites More sharing options...
Mchl Posted February 2, 2010 Share Posted February 2, 2010 You should certainly look into patterns. In my opinion they show what OOP is really about. As to your book: I don't know it, so I can't tell whether it's good or not. Quote Link to comment https://forums.phpfreaks.com/topic/190701-need-very-quick-advice/#findComment-1005724 Share on other sites More sharing options...
gizmola Posted February 2, 2010 Share Posted February 2, 2010 Sorry... I did take what Mchl said and have taken his advice... sorry for not providing any recognition.. :-\ In terms of my class variables, I was under the impression that any variables that use inside the class should be declared at the beginning?? Are you saying that none of the variables I am using are needed to be defined, I can just use normal variables without declaring them? Thanks for everyones input. Garry I guess you may have tried, but I didn't see what I was expecting, which was that you would have a protected variable called something like "$comments" that would be an array of the comments related to the blog post. You would then provide some mechanism that would load the comments in when the blog post is loaded. That could be via a seperate comment class or a method in your current blog class. As for my comment about variables, my point is that your constructor does not set those variables. Instead as Mchl opined, it seems that your class is more a set of functions than something that looks like a blog class. The other thing you're doing that I wouldn't do, is you're tying the details of the presentation of data to the data itself. It's better to have the data in some sort of raw form, and then you can either have seperate presentation methods, or better yet, utilize a view class with one or more templates that handle the markup. If you were to remove the presentation stuff, you'd have something that typically would go into the category of a "model" since you asked about MVC frameworks. In terms of looking at how skilled practitioners do it, definately take a look at the Zend Framework. They have used well established design patterns throughout. Quote Link to comment https://forums.phpfreaks.com/topic/190701-need-very-quick-advice/#findComment-1005761 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.