Jump to content

Need Very Quick Advice!!??


gaza165

Recommended Posts

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.

Link to comment
Share on other sites

<?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>";
	}
}



?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.