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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

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

Post 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 :)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Quick Q:

Would this not be more efficient?

Code: Select all

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

:)
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

I can just concatenate the two variables, and I don't need to use sprintf?
Post Reply