I've not played around with functions/classes etc for a while. Still very much a beginner but managing to get a vague grasp for the very basics e.g. passing variables etc. Anyway, so I'm only writing very basic scripts just in my own time to get a feel for things and seeing how useful it actually is. I've decided that for this example, I want to store all my forms in functions. 1. Is this a good idea? 2. If so, is this a good example? index.php <?php require_once('classes.php'); require_once('foms.php'); loginForm(); $page4->showPage(); ?> PHP: forms.php <?php function loginForm(){ if(isset($_POST['submit'])) { doLogin("$_POST[user]","$_POST[pass]"); } else { echo ' <form method="post"> <input type="text" name="user"><br /> <input type="text" name="pass"><br /> <input type="submit" name="submit" value="submit"> </form> '; } } function doLogin ($username,$password){ if ($username == "scott" && $password == "pass"){ echo " Success, you are logged in as $username "; }else { } } ?> PHP: Please note, I'm not hooking it up to a database hense having username/password in the script without escaping etc (I'd be using PDO for that).
It depends only on your professional level. Because for php guru your idea sucks, for beginner it seems not bad
A better way to handle this would be in a framework like Code Igniter. This will teach you MVC (Model View Controller) which is certainly the way you should be creating applications. MVC pretty much means keep your code split between: Logic - (Codeigniter Controllers) Database - (Codeigniter Models) Display - (Codeigniter Views) Have a look at this: http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Other than that your code was okay for a PHP 4 project, but now days clients want everything in PHP5 and in some kind of framework. Good luck.
What answer you expected to hear? You code is ok for beginner. But professional developer would use some framework, or self-written MVC/HMVC structure, some templater like Smarty.
But would it not be reasonable to grasp the concepts of OOP before learning a framework that uses OOP? I'm trying to learn where and when to use it. If I'm just learning to pass variables through, how can you reasonably suggest I now go and learn a framework and with no answer relating to the OP?
I would agree with using a Framework, but storing forms as functions doesn't seem really useful to be quite honest. So to answer your question from a personal view, It's a bad/useless idea. It's not good. Why? Bad Practice I reckon.
If I was in your position which I was a long time ago I would have a look at this: http://ellislab.com/codeigniter/user-guide/tutorial/index.html This will pretty much sort out any questions/concerns you have about OOP and MVC.
<?php /** An example for 'scottlpool2003' from DigitalPoint. Purpose, create a form using OOP (not even getting into FULL oop, just a basic level) vickkumar2011@gmail.com - for help, etc. by Vick.Kumar =) */ class forms { function openform($method, $action) { print '<form method="'.$method.'" action="'.$action.'">'; } function input($text, $type, $size, $name, $id) { print $text. ' <input type="'.$type.'" size="'.$size.'" name="'.$name.'" id="'.$id.'"/><br />'; } function submit() { print '<input type="submit" value="submit" />'; } function closeform() { print '</form>'; } } // $newform = forms class $newform = new forms(); // open form html, select post/get, insert action location echo $newform->openform("post", "index.php"); // create new input, 'text to display', 'type', 'size', 'name', 'id' echo $newform->input("Name", "text", "20", "name", "name"); echo $newform->input("Email", "text", "20", "name", "name"); echo $newform->input("Age", "text", "20", "name", "name"); // throw out a html submit button echo $newform->submit(); // close the form echo $newform->closeform(); PHP: Here you go mate. I took 5 minutes of my life to write you something simple for you to read, and understand, learn from. I hope it helps. You can modify it, and play with it so you can learn.
I've looked into Codeigniter but run straight into problems when it comes to routing as I'm running a Windows server. This also limits a lot of the tutorials I find.