Page 1 of 1

Show me how to turn this into a class please

Posted: Wed Jun 21, 2006 5:27 pm
by Benjamin
Classes, or groups of functions will come in handy as I start to build my own custom framework. This is my theory at least. So with that in mind, can someone please show me how to turn this into a simple class? This is an oversimplified version of course, I just need to know the flow and syntax so that I can start writing classes.

Code: Select all

<form name="login" action="<?php echo $Action; ?>" method="<?php echo $Method; ?>">
            <table cellpadding="0" cellspacing="0" border="0">
              <tr>
                <th colspan="2">Login</th>
              </tr>
              <tr>
                <td class="left">Username<span class="red">*</span></td>
                <td><input type="text" name="Username" value="" maxlength="96" /></td>
              </tr>
              <tr>
                <td class="left">Password<span class="red">*</span></td>
                <td><input type="password" name="Password" value="" maxlength="255" /></td>
              </tr>
              <tr>
                <td class="left">&nbsp;</td>
                <td><div id="CheckBox"><input class="checkbox" type="checkbox" name="RememberMe" value="True" /><span>Remember Me</span></div></td>
              </tr>
              <tr>
                <td class="left">&nbsp;</td>
                <td><input class="submit" type="submit" name="Action" value="Login" /></td>
              </tr>
            </table>
          </form>
So I can call it with something like (sudo)

Code: Select all

new login();
this->Action = 'blah';
this->Method = 'blah';

Re: Show me how to turn this into a class please

Posted: Wed Jun 21, 2006 5:31 pm
by Christopher
Do you want a general purpose form building class or a View class that displays that specific form?

Posted: Wed Jun 21, 2006 5:32 pm
by Benjamin
Umm, I'm not sure. Basically just a way for me render the form by calling the class and insert some values into it. Although I'll probably turn it into a form building class...

Posted: Wed Jun 21, 2006 6:48 pm
by Benjamin
Well I wrote my first class... it's very simple but I know how to make them now at least...

Code: Select all

class LoginForm {
  function DisplayForm() {
    ?>
          <form name="login" action="<?php echo $this->Action; ?>" method="post">
            <input type="hidden" name="Login" value="Go" />
            <table id="form" cellpadding="0" cellspacing="0" border="0">
              <tr>
                <th colspan="2">Login</th>
              </tr>
              <tr>
                <td class="left">Username<span class="red">*</span></td>
                <td><input type="text" name="Username" value="" maxlength="96" /></td>
              </tr>
              <tr>
                <td class="left">Password<span class="red">*</span></td>
                <td><input type="password" name="Password" value="" maxlength="255" /></td>
              </tr>
              <tr>
                <td class="left">&nbsp;</td>
                <td><div id="CheckBox"><input class="checkbox" type="checkbox" name="RememberMe" value="True" /><span>Remember Me</span></div></td>
              </tr>
              <tr>
                <td class="left">&nbsp;</td>
                <td><input class="submit" type="submit" name="Action" value="Login" /></td>
              </tr>
            </table>
          </form>
    <?php
  }
}
And I called it like...

Code: Select all

$Login = new LoginForm;
      $Login->Action = "test.php";
      $Login->DisplayForm();

Posted: Wed Jun 21, 2006 7:25 pm
by Christopher
astions wrote:Well I wrote my first class... it's very simple but I know how to make them now at least...
Its a start. You should define the properties. One things about a class is that it should be generally initialized to reasonable defaults. For example:

Code: Select all

class LoginForm {
  var $name;
  var $action;
  var $method;

  function LoginForm ($name, $action=null, $method = 'post') {
    $this->name= $name;
    if ($action) {
      $this->action = $action;
    } else {
      $this->action = $_SERVER['SCRIPT_NAME'];
    }
    $this->method = $method;
  }

  function display() {
    ?>
          <form name="<?php echo $this->name; ?>" action="<?php echo $this->action; ?>" method="<?php echo $this->method; ?>">
            <input type="hidden" name="Login" value="Go" />
            <table id="form" cellpadding="0" cellspacing="0" border="0">
              <tr>
                <th colspan="2">Login</th>
              </tr>
              <tr>
                <td class="left">Username<span class="red">*</span></td>
                <td><input type="text" name="Username" value="" maxlength="96" /></td>
              </tr>
              <tr>
                <td class="left">Password<span class="red">*</span></td>
                <td><input type="password" name="Password" value="" maxlength="255" /></td>
              </tr>
              <tr>
                <td class="left">&nbsp;</td>
                <td><div id="CheckBox"><input class="checkbox" type="checkbox" name="RememberMe" value="True" /><span>Remember Me</span></div></td>
              </tr>
              <tr>
                <td class="left">&nbsp;</td>
                <td><input class="submit" type="submit" name="Action" value="Login" /></td>
              </tr>
            </table>
          </form>
    <?php
  }
}

      $Login = new LoginForm('test', 'test.php');
      $Login->display();

Posted: Wed Jun 21, 2006 7:39 pm
by Chris Corbyn
Hopefully not jumping into deep water too quickly here but it's generally a good idea to keep markup to a minimum in your classes. Simple things like <strong>Error here</strong> and wot-not are fine but full-blown templates are better suited to being in a seperate file.

For all the time I spent trying to come up with neat ways to template I've finally decided I must have been going nuts considering it's much faster to just use native PHP constructs and varaibles in templates. That's out of the scope of this thread however.

I generally write a simple class that handles login. It scans for POST vars of the correct names, checks them against a DB record and sets a session var (or a few) if everything is authenticated. The login class never needs to be called again since the application components then check for the existence of the session var by using a loggedIn() method.

Is it the login stuff you're more interested in or the HTML that is presented to the user?

Posted: Wed Jun 21, 2006 7:43 pm
by Weirdan
For all the time I spent trying to come up with neat ways to template I've finally decided I must have been going nuts considering it's much faster to just use native PHP constructs and varaibles in templates.
PHP is a great templating engine by itself :)

Posted: Wed Jun 21, 2006 7:51 pm
by Benjamin
arborint wrote: One things about a class is that it should be generally initialized to reasonable defaults
Ok. I see the..

Code: Select all

var $name;
  var $action;
  var $method;
Which I'm guessing creates the variables? I read in another tutorial that this makes them global? I don't want them global. Unless this is required to make them global in the class, which I thought they all were anyway?

Code: Select all

function LoginForm ($name, $action=null, $method = 'post') {
    $this->name= $name;
    if ($action) {
      $this->action = $action;
    } else {
      $this->action = $_SERVER['SCRIPT_NAME'];
    }
    $this->method = $method;
  }
Ok so if I create a function with the same name as the class it will get executed when I call the class. If $action is null does that evaluate to false? I would assume so based on the code you wrote.
d11wtq wrote: Hopefully not jumping into deep water too quickly here but it's generally a good idea to keep markup to a minimum in your classes. Simple things like <strong>Error here</strong> and wot-not are fine but full-blown templates are better suited to being in a seperate file.
Yeah I know, I'm just teaching myself the structure of a class and how to call it. Once I understand it, I'll build classes that create the markup. I'm assuming I can call a seperate class from within another class...
d11wtq wrote: Is it the login stuff you're more interested in or the HTML that is presented to the user?
Both, I'll seperate them once I get the hang of it.

Posted: Wed Jun 21, 2006 7:54 pm
by Weirdan
read in another tutorial that this makes them global?
They aren't global since they're declared inside the class definition. You would access them with a $this->variable_name syntax from inside the class methods and $obj->variable_name syntax from outside.

Posted: Wed Jun 21, 2006 8:12 pm
by Christopher
Weirdan and d11wtq make an excellent point (as usual) that it is a good practice to separate you markup/template/presentation stuff separate from your actual program. A next step might be something like this:

Code: Select all

class TemplateForm {
  var $name;
  var $action;
  var $method;
  var $filename;

  function TemplateForm ($name, $action=null, $method = 'post') {
    $this->name= $name;
    if ($action) {
      $this->action = $action;
    } else {
      $this->action = $_SERVER['SCRIPT_NAME'];
    }
    $this->method = $method;
  }

  function setTemplate($filename) {
    $this->filename= $filename;
  }

  function display() {
    include $this->filename;
   }
}

$Login = new TemplateForm('test', 'test.php');
$Login->setTemplate('templates/login.php');
$Login->display();
login.php

Code: Select all

<?php
          <form name="<?php echo $this->name; ?>" action="<?php echo $this->action; ?>" method="<?php echo $this->method; ?>">
            <input type="hidden" name="Login" value="Go" />
            <table id="form" cellpadding="0" cellspacing="0" border="0">
              <tr>
                <th colspan="2">Login</th>
              </tr>
              <tr>
                <td class="left">Username<span class="red">*</span></td>
                <td><input type="text" name="Username" value="" maxlength="96" /></td>
              </tr>
              <tr>
                <td class="left">Password<span class="red">*</span></td>
                <td><input type="password" name="Password" value="" maxlength="255" /></td>
              </tr>
              <tr>
                <td class="left">&nbsp;</td>
                <td><div id="CheckBox"><input class="checkbox" type="checkbox" name="RememberMe" value="True" /><span>Remember Me</span></div></td>
              </tr>
              <tr>
                <td class="left">&nbsp;</td>
                <td><input class="submit" type="submit" name="Action" value="Login" /></td>
              </tr>
            </table>
          </form>

Posted: Thu Jun 22, 2006 3:27 am
by Maugrim_The_Reaper
You should really read up on OOP basics - not being an evil mad sitting-on-a-throne creature of darkness (well, I am - see avatar) but it's easy to get into to, and applicable across a few languages. That said Aborint's last example is sort of showing you the path. Once you remove the markup to a separate file, the class instantly becomes more readable and adaptable. A class should be reuseable, and the above illustrates that nicely. In its simple form, you can use the above class for any form - just make a template for it. With the original approach, you would have needed a new class for every form - which would be very time consuming to maintain.

Posted: Thu Jun 22, 2006 10:34 am
by Benjamin
Not to worry. I just wanted a quick and dirty start. I'll most likely start writing some classes soley designed for markup, unless I go with a template based system.