Jump to content

Class what is best practise? form->handler->class->db


Ansego

Recommended Posts

Hello all; might be a silly question but I need to ask it, I end up with 1000+ lines of code for 10 field forms, thinking to myself this cant be right, defiantly not using the KISS principle!, so I want to try using classes and not 100% on structure or use and hoping they may make my life easier.

 

Below is MOCK up code and my questions are:

 

Q: Is this best practise? (form->handler->classes->db) REF BELOW CODE!

Q: How do you guys handle your forms -> classes -> db?

Q: What is best practise?

Q: In PAGE: index.php should it be including all 3 other pages? is that how you include a class?

Q: Please share rules and thumbs, hints, theories, methodologies etc etc...

 

MOCK CODE - CODE NOT TESTED & Prob won't work just general mock for structure and logic:

 

PAGE: INDEX.PHP (MOCK CODE) 

include 'inc/frm_handler.inc.inc';
include 'inc/check_class.inc';
include 'inc/dbhandler_class.inc';

<form...>
<input name='test' type='text' value=''/>
<input type='button' value='&Submit'/>
</form>

PAGE: FORM_HANDLER.INC (MOCK CODE)

$testcheck = new testcheck();
$myvar = $_Post['test'];

if ($testcheck.test_check($myvar) == true){
         $dbsqlsend = new dbsqlexecute();
         $dbsqlsend.executeSQL($myvar);
	
}

PAGE: CHECK_CLASS.INC (MOCK CODE)

class testcheck{

	public test_check($str_check){
	
		if (filter_var($str_check, FILTER_SANITIZE_STRING)){
				return true;
			} else {
				return false;
		};
	};
}; 

PAGE: DBHANDLER_CLASS.INC (MOCK CODE)

class dbsqlexecute{

	public executeSQL($str_sql){
		// EXECUTE MYSQLI CODE HERE ETC
	};
};

Q: Is this best practise? (form->handler->classes->db) REF BELOW CODE!

It is one way of handling things, and is probably pretty close to how most frameworks would handle it.

 

 

 

Q: How do you guys handle your forms -> classes -> db?

Most frameworks have some form of "form" handling built in.

 

Q: What is best practise?

Don't reinvent the wheel.

 

Q: In PAGE: index.php should it be including all 3 other pages? is that how you include a class?

No and No. Classes should be auto loaded using an autoloader (see spl_autload)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.