Jump to content

OK, i got this far, what now?


Perad

Recommended Posts

I decided i would make my fledgling blogging application take on OOP standards. Currently it is just grabbing things from a database i thought it would be better to convert it while it is this simple.

 

I have since converted my script into what i believe is a workable class. Now i just don't quite get what to do with the class.

 

What my class does

 

Firstly it gets $id and $all from the URL and determines if they exist.

 

It then decides what additional SQL statement is needed

 

A function then runs a database query which is a default query plus whatever the above generated. This function then loops out all of the results and puts them into arrays. I then assign the variables to smarty variables so i can use them in template.

 

Questions

 

I want it run through the whole class. Does this mean i have to execute every function?

Any chance someone could give me a brief example of how I would run my class to do what it is supposed to do?

 

Class

<?php
require '../smarty/libs/Smarty.class.php';

# New Smarty
include ('../mysql_connect.php');

$smarty = new Smarty;

class FilterPage{	
private $all;
private $id;
private $SQLExt;
public $SQLError;

public function __getpage {
	$this->all = $_GET['all'];
	$this->id = $_GET['id'];
	if(!$this->all) {
		$this->all = FALSE;
	} else {
		$this->all = TRUE;
	}
	if(!$this->id) {
		$this->id = FALSE;
	} else {
		$this->id = $_GET['id'];
	}
}

public function __findPage($all,$id) {
	if ($this->all==NULL && $this->id==NULL) {
		$this->SQLExt = "LIMIT 5";
	} else if {$this->all==TRUE && $this->id==NULL) {
		$this->SQLExt = " "; 
	} else if {$this->id!=NULL && $this->all==FALSE) {
		$this->SQLExt = "WHERE blog_id='$id'";
	} else {
		$this->SQLError = "Page identity error. Click <a href=\"blog.php\">here</a> to continue.</a>";
	}
}

public function __findBlogItems ($SQLExt) {
	$sql="SELECT * FROM blog ". $this->SQLExt . " ORDER BY blog_postdate DESC";
	$result = mysql_query($sql) or die(mysql_error());	

	while ($row = mysql_fetch_assoc ($result)) {
		$blog_id[] = $row['blog_id'];
		$blog_title[] = $row['blog_title'];
		$blog_meta[] = $row['blog_meta'];
		include_once("../textilephp/Textile.php");
		$text = $row['blog_content'];
		$textile = new Textile;
		$blog_content[] = $textile->process($text);	
		$blog_postdate[] = date("d-m-Y",strtotime($row['blog_postdate']));
	}

	$smarty->assign("blog_id", $blog_id);
	$smarty->assign("blog_title", $blog_title);
	$smarty->assign("blog_meta", $blog_meta);
	$smarty->assign("blog_content", $blog_content);
	$smarty->assign("blog_postdate", $blog_postdate);
	$smarty->assign("blog_author", "Jason");
}
}

$blog = new FilterPage;

Link to comment
Share on other sites

If you are wanting to run the entire class at start up (like it's a system or something and everything need's to be run at system startup everytime. Here is a quick idea.

1. Instantiate the class

2. After the instance is created, get an array together of all the function names within your class (all the ones you need to run)

3. Run a foreach on the loop running each function one at a time.

like

$class = new class();

$functions = array("start", "afterstart", "middle", "aftermiddle", "end", "afterend"); // just some examples

foreach ($functions as $k => $v) {

  $k . "()";

}

Yeah, that probably won't work without some thought.  However you get the idea of what I meant anyway.

Link to comment
Share on other sites

The answers to some of your questions are things only you can answer. Instead of asking how to do something, you need to have a very clear picture in your mind of what your goals with this class are. IMO, your class should never access the global variables (such as $_GET and $_POST) directly, unless it is within a function built to filter user input. Also, remember that a class in and of itself is an attempt to remove some of the complexity of the code away from the user. So, here are some very basic modifications I would do to a basic blogging class like that:

<?php
// Alterations to class declaration
class FilterPage {	
  protected $all = TRUE;
  protected $id  = FALSE;
  protected $SQLExt = "LIMIT 5";

  public function __construct() {
    $this->getpage();
    $this->findPage();
  }

  protected function getPage() {
    if (isset($_GET['all'])) $this->all = !$_GET['all'] ? FALSE : TRUE;
    if (isset($_GET['id']) && is_numeric($_GET['id'])) $this->id = $_GET['id'];   
  }

  public function findPage() {
    if ($this->all && !$this->id) $this->SQLExt = " "; 
    else if (!$this->id && !$this->all) $this->SQLExt = "WHERE blog_id='{$this->id}'";
    else throw Exception("Page identity error. Click <a href=\"blog.php\">here</a> to continue.</a>");
  }
}

// Class in action
try {
  $page = new FilterPage();
} catch (Exception $e) {
  $error = $e->getMessage();
}
?>

 

Notice how I'm defaulting your variables within your class to the default values you were after in your functions. This way, you can avoid some of the additional conditionals. Also, if you use Exceptions in PHP5, you can avoid having to handle your own errors within the class as well. As for running individual functions when you create the object, you simply have to call those functions within your constructor. This will give you control over what happens when the class is created and what you want to call manually from your page.

 

Hope this helps some.

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.