4

When I try to submit a form on my site I get the following error:

Fatal error: Cannot access empty property in functions/form_validation.php on line 15

The code for form validation is:

class Validation {
    var $success;
    var $post_data;
    var $errors;
    var $delimiter;


    function Validation($HTTP_POST_DATA) {
        $this->success = true;
        $this->errors = false;
        $this->$post_data = $HTTP_POST_DATA;
        $this->delimiter = "|";
        while(list ($key, $val) = each ($this->$post_data)) {
            $this->{$key} = $val;
        }
        reset($this->$post_data);
        while(list ($key, $val) = each ($this->$post_data)) {
            if(method_exists($this, $key)) {
                $cmd = "\$this->".$key."();";
                eval($cmd);
            }
        }
    }

The code from the form page is:

 include_once($APP_ROOT . "functions/index.php");
    include_once($APP_ROOT . "functions/form_validation.php");
    $CONTINUE = TRUE;
    $valid = new Validation($_POST);
    if($CONTINUE = $valid->success) {

This code used to work just fine before we upgraded to PHP5. Any idea what I need to change to get it working again?

Thanks

  • 3
    Since your lines are not numbered here, and we don't know if there's more code around that, can you point out which of those lines is throwing the error? – Geeky Guy Sep 18 '13 at 19:07
  • Are you using PHP 4?. If not then, read the documentation http://php.net/manual/en/language.oop5.php You don't have to use `var` anymore. You all can use `public function __construct() {}` as your constructor. – Ryan Sep 18 '13 at 19:09
  • Also, as a side note, there is no need to use eval. Instead of: `$cmd = "\$this->".$key."();"; eval($cmd);` Simply do: `$this->$key()` – Mark Sep 18 '13 at 19:40

3 Answers3

7

$this->$post_data should be $this->post_data

In future, try looking at the line that the error message points you do, because usually that's where the problem is! ;)

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • Thanks. Changing every instance of $this->$post_data should be $this->post_data worked. Can you explain to me in simple terms why I needed to make that change after going from PHP4 to PHP5? – user2791808 Sep 18 '13 at 19:15
  • Um... I'm not sure why I should have to, since you have plenty of other property accesses right there, but for some reason you put an extra `$` in that specific one... – Niet the Dark Absol Sep 18 '13 at 20:20
6

Change this:

$this->$post_data = $HTTP_POST_DATA;

To this:

$this->post_data = $HTTP_POST_DATA;
Mark
  • 1,376
  • 9
  • 16
  • Thanks. Changing every instance of $this->$post_data should be $this->post_data worked. Can you explain to me in simple terms why I needed to make that change after going from PHP4 to PHP5? – user2791808 Sep 18 '13 at 19:20
  • 1
    To be perfectly honest, I have no idea, though I have absolutely no experience with PHP4. I would however, love to hear why this ever worked. – Mark Sep 18 '13 at 19:36
1

You can take care of the immediate problem by modifying the line to read:

if(property_exists(this, $key)) $this->{$key} = $val;

If you'd like to know exactly WHY this isn't working try printing the POST array in debug mode:

function Validation($HTTP_POST_DATA) {
  echo '<pre>'. print_r($HTTP_POST_DATA, true). "</pre>\n";
... rest of method ...

Also, change all instances of $this->$post_data to $this->post_data

krowe
  • 2,129
  • 17
  • 19