Page 1 of 1

Defining $_POST vars inside a class to avoid errors?

Posted: Wed May 02, 2007 4:05 am
by RecoilUK
Hi guys

I have a problem, here is the code ...

Code: Select all

require_once('dbconnect.php');
include 'header.php';

class Market {

	var $submit;

	function __construct() {
	
		$this->submit = $_POST['Submit'];
	}
Now, i,m getting a notice about using a undefined variable in the class (the $_POST var), and whilst this doesnt stop the script from functioning I would like to get rid of it.

Can anyone tell me how to do this?

Thanks

Posted: Wed May 02, 2007 5:39 am
by CoderGoblin

Code: Select all

if (!empty($_POST['submit'])) {
    $this->submit = $_POST['Submit']; 
  } else {
    $this->submit = ''; // default value
  }
Normally I would never use $_POST in a class. You are tying the class to a specific project/HTML layout restricting it's reuse.

Posted: Wed May 02, 2007 6:08 am
by RecoilUK
Hi

Thanks for the reply.

Maybe I am wrong in the way classes are supposed to be used.

Could you give a brief description on what you do and do not include within a class?

Thankyou.

Posted: Wed May 02, 2007 6:36 am
by CoderGoblin
What you do/do not do is very much a matter of personal opinion. Indeed the whole issue of Functions vs Classes is often argued about. I am of the opinion that both have their place. I like to make any class part of the "business logic" and as "independant" of things like HTML as possible to assist in reuse. I believe it's called "loose coupling" and I know there are lots of people on this board who are more up on something called "design patterns" and all the latest terminology than I am.

To put it in a nutshell you call the class from another piece of code. This code is the one that is responsible for checking the $_POST variable and if necessary passes the value into the class constructor. After all if it doesn't exist isn't this an error or isn't it the case that the class is not needed at the current time? Why spend additional time loading and processing it ?

If you change the name of the HTML form button for one page/project and your class is used on several pages/projects, either you have to change all the buttons if you can find them or have different versions of the class which is a maintenance nightmare.

One of the questions you need to ask is why am I making this a class and not just a group of functions ? If you are not careful (and I started when classes came out to do this myself :oops: ) you make classes where one or two common functions is all that is actually needed and these functions have nothing to do with one another.

Posted: Wed May 02, 2007 7:22 am
by feyd
The discussion between procedural and object oriented development (functions versus classes) has been argued multiple times here. It would probably be a good idea to seek them out to get an understanding of the entire issue.

Posted: Wed May 02, 2007 8:26 am
by neel_basu

Code: Select all

<?php
class Market
  {
    var $submit;
    function __construct()
      {
        $this->submit = $_POST['Submit'];
      }
  }
$tst = new Market();
print_r($tst->submit);
?>
It will work. If the form has been posted

Posted: Wed May 02, 2007 12:22 pm
by jmut
Here is sample code that could help you address the problem OOP wise.

Pros:
1. Easy to "teach" the class how to take cli params. Because just like post, get... cli passes params to the script.
Interface will be kept the same. ->get() ->set()
I would say this is the main benefit to making this OOP like.
2. Easy avoid of all noticies...can supply default values etc...can be easily done with simple funciton
3. Easy to add callBackFunctions. Like trim() strip_tags or anything that will make sense in you APP.

Cons:
1. A bit more work at the beginning.

There are always pros and cons...you just have to decided whats best in your case.

Code: Select all

<?php

class Request
{

    // {{{ properties

    /**
     * Array with request data. e.g $_REQUEST
     *
     * @var array
     */
    private $fields;

    static private $instance = null;

    // }}} properties
    // {{{ __construct()

    /**
     * Singleton pattern
     *
     */
    private function __construct()
    {
        $this->init();
    }
    private function __clone() {}

    // }}} __construct()
    static public function getInstance()
    {
        if (! self::$instance) {
          self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     * Here could implement init() to know cli params.
     *
     */
    private function init()
    {
      if (isset($_SERVER['REQUEST_METHOD'])) {
          $this->fields = $_REQUEST;
          $r = array();
          foreach ($this->fields as $fieldName=>$fieldValue) {
            $r[$fieldName] = trim($fieldValue);
          }
          $this->fields = $r;
      }
    }
    // {{{ get()

    public function get($fieldName = null,$defaultValue = '')
    {
      if (is_null($fieldName)) return $this->fields;

      if (isset($this->fields[$fieldName])) {
          return $this->fields[$fieldName];
      }

      return $defaultValue;
    }

    // }}} get()
    // {{{ set()

    public function set($fieldName,$value)
    {
      $this->fields[$fieldName] = $value;
    }

    // }}} set()
}

// }}} Request

$request = Request::getInstance();
/*@var $request Request */
$request->get('Submit');//get var as is.
?>

Posted: Wed May 02, 2007 12:44 pm
by superdezign
When dealing with posted variables in a class, I sometimes just have a function that takes the $_POST array as a parameter, then validates and all from within the function. Then, use a switch statement on the result in the HTML (*Ahem* PHP) to determine the error.