Page 1 of 1

I wrote this sweet class. It handles data from forms. Sort o

Posted: Fri Jun 30, 2006 3:58 pm
by daedalus__

Code: Select all

class FormHandler
{
	public function __construct()
	{
		$this->query	= !empty($_GET['q']) ? $_GET['q'] : 'home';
		$this->action	= !empty($_GET['a']) ? $_GET['a'] : 'default';
		$this->id	= !empty($_GET['id']) ? $_GET['id'] : '';
	}
	
	// Check for $_POST values
	public function IsPost()
	{
		if (isset($_POST['submit']) != FALSE)
		{
			$this->ChooseMethod($this->query, $this->action, $this->id);
		}
	}

	// Have the class find and use the appropriate method
	private function ChooseMethod($q, $a, $id = NULL)
	{
		$class_name	= sprintf("%s", $q);
		$class = new $class_name;
		
		$method_name	= sprintf("%s%s", $a, $q);
		$class->$method_name();
	}
}
It's kind of like a valet or something. I write a class for every part of the website where form data is handled and then this thing inits the class and runs the method. As long as I follow the name convention I've come up with for this, the whole thing should work fine, everytime.

This is a really early, early version that I just wrote today.

I'll take any and all suggestions :)

Posted: Fri Jun 30, 2006 4:04 pm
by Jenk
Quick Q:

Would this not be more efficient?

Code: Select all

$class_name     = $q;
and likewise for the other sprintf?

:)

Posted: Fri Jun 30, 2006 4:09 pm
by daedalus__
I can just concatenate the two variables, and I don't need to use sprintf?