Defining $_POST vars inside a class to avoid errors?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
RecoilUK
Forum Commoner
Posts: 30
Joined: Sun Feb 29, 2004 7:13 pm

Defining $_POST vars inside a class to avoid errors?

Post 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
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.
RecoilUK
Forum Commoner
Posts: 30
Joined: Sun Feb 29, 2004 7:13 pm

Post 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.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post 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
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post 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.
?>
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
Post Reply