Functions/Classes Question

Discussion in 'PHP' started by scottlpool2003, Mar 27, 2013.

  1. #1
    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).
     
    scottlpool2003, Mar 27, 2013 IP
  2. IGarret

    IGarret Active Member

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    3
    Trophy Points:
    51
    #2
    It depends only on your professional level. Because for php guru your idea sucks, for beginner it seems not bad :)
     
    IGarret, Mar 27, 2013 IP
  3. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #3

    What a ridiculously unhelpful answer!
     
    scottlpool2003, Mar 27, 2013 IP
  4. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #4
    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.
     
    HuggyStudios, Mar 27, 2013 IP
  5. IGarret

    IGarret Active Member

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    3
    Trophy Points:
    51
    #5
    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.
     
    IGarret, Mar 27, 2013 IP
  6. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #6

    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?
     
    scottlpool2003, Mar 27, 2013 IP
  7. Vick.Kumar

    Vick.Kumar Active Member

    Messages:
    138
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    90
    #7
    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.
     
    Vick.Kumar, Mar 27, 2013 IP
  8. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #8

    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. :)
     
    HuggyStudios, Mar 27, 2013 IP
  9. Vick.Kumar

    Vick.Kumar Active Member

    Messages:
    138
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    90
    #9
    <?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. :)
     
    Vick.Kumar, Mar 27, 2013 IP
    scottlpool2003 likes this.
  10. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #10

    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.
     
    scottlpool2003, Mar 27, 2013 IP