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
RecoilUK
Forum Commoner
Posts: 30 Joined: Sun Feb 29, 2004 7:13 pm
Post
by RecoilUK » Thu Oct 19, 2006 12:24 am
Hi guys
Got a problem and I hope someone can help.
Basically i,m designing a form handler class.
The construct takes a form name as a value, based on that value, I want to run a pre-defined class function.
Is this possible?
Here's what I have so far ...
Code: Select all
<?php
class form_handler {
var $form_name;
var $form_state;
var $form_error_count = 0;
var $form_errors_array = array();
function __construct($formname) {
$this->form_name = $formname . '_form.tpl.php';
<---code here to call $this->_registration();--->
}
function _showform() {
include './templates/' . $this->form_name;
}
function _registration() {
if (!isset($_POST['submit'])) {
$this->_showform();
}
elseif (isset($_POST['submit'])) {
$this->_form_val_required('name','password','email');
if (!$this->form_error_count == 0) {
$this->_showform();
}
}
}
function _form_val_required() {
$numargs = func_num_args();
for ($i = 0; $i <= $numargs - 1; $i++) {
if ($_POST[func_get_arg($i)] == "") {
$this->form_errors_array[func_get_arg($i)] = "<div class=\"formerror\">The field above is required!</div>\n";
$this->form_error_count++;
}
}
}
}
?>
Thx guys
jmut
Forum Regular
Posts: 945 Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:
Post
by jmut » Thu Oct 19, 2006 1:40 am
I would suggest taking each form's specifics as a separate class and have a well defined interface they all obey to.
for example (not tested)
Mind the comments...if using php5 you should implement them.
Code: Select all
<?php
/**
* @abstract
*/
class Form_Interface
{
/**
* @final
* @static
* @access public
*/
function factory($form_name) {
switch ($form_name) {
case 'step1':
$obj = new Form_Step1();
return $obj;
break;
case 'step2':
$obj = new Form_Step2();
return $obj;
break;
default:
//error ...no such form.
break;
}
}
/**
* @abstract
* @access public
*/
function registration()
{
die("Abstract method");
}
/**
* @abstract
* @access public
*/
function showform()
{
die("Abstract method");
}
/**
* @access protected or public
*/
function commonStuff()
{
//some stuff that step1 form and step2 form do the same way. avoid duplication.
}
}
/**
* @access private
*/
class Form_Step1 extends Form_Interface
{
/**
* Implements abstract method
* @access public
*/
function registration()
{
//do some stuff concerning step1 form
}
}
/**
* @access private
*/
class Form_Step2 extends Form_Interface
{
/**
* Implements abstract method
* @access public
*/
function registration()
{
//do some stuff concerning step2 form
}
}
/**
* Example code
*/
/* @var $oForm Form_Interface*/
$oForm = Form_Interface::factory('step1');
$oForm->registration();
$oForm->showform();
?>
RecoilUK
Forum Commoner
Posts: 30 Joined: Sun Feb 29, 2004 7:13 pm
Post
by RecoilUK » Thu Oct 19, 2006 2:10 am
Hi jmut
Thx for the response, and while I appreciate the effort you have given, for the time being I want to continue with what I have if I can get it working.
When I have it working, I will then look into rewriting the class, as at the moment I have no idea of what the difference is between a normal class and an abstract class, and I find your explanation somewhat confusing, as all i,m passing the class is a form name, so I dont know where your getting Step 1, Step 2 from, and I want to actually code it, because if I have to keep stopping to learn extra functionality, PHP 6 will be out and i,ll have to start again.
I,m sure you understand.
Thx
jmut
Forum Regular
Posts: 945 Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:
Post
by jmut » Thu Oct 19, 2006 2:14 am
ok...maybe I didn't get the problem but...as far as I see it.
you want to run different _registration() method depending on the form_name right?
so using simple inheritance in this case should work.
just each form_name is actually a class.
and they all call __registration()
only in each case __registration() will do different stuff (depending on the form_name).
Another way(bad) to do this is.
in __registration() method you make if/else depending on the form_name
edit: step1, step2 is actually different form_names you have.....I just named them step1 and step2..
Last edited by
jmut on Thu Oct 19, 2006 2:23 am, edited 1 time in total.
RecoilUK
Forum Commoner
Posts: 30 Joined: Sun Feb 29, 2004 7:13 pm
Post
by RecoilUK » Thu Oct 19, 2006 2:22 am
Hi
Sorry, maybe my description was bad also.
I,m actually supplying the formname, in the example it is registration.
So based on that I want to call $this->_registration();
If it was passed login, it would call the $this->_login(); function which isnt written yet.
Its just I dont know how to call a function based on the formname i,m supplying.
Thanks
jmut
Forum Regular
Posts: 945 Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:
Post
by jmut » Thu Oct 19, 2006 3:24 am
RecoilUK wrote: Hi
Sorry, maybe my description was bad also.
I,m actually supplying the formname, in the example it is registration.
So based on that I want to call $this->_registration();
If it was passed login, it would call the $this->_login(); function which isnt written yet.
Its just I dont know how to call a function based on the formname i,m supplying.
Thanks
Code: Select all
$method = 'login';
$this->$method()
RecoilUK
Forum Commoner
Posts: 30 Joined: Sun Feb 29, 2004 7:13 pm
Post
by RecoilUK » Thu Oct 19, 2006 5:06 am
Hi
Easy when you know how, I must have tried every combination I could think of apart from that.
Now as far as the class is concerned, what do you think of it?
Would you consider having a seperate function for each form a good idea?
Obviously there are going to ba a few different validation classes also.
Thanks for your help, its appreciated.
L8rs