-3

don't know better title for this, but here's my code.

I have class user that checks form data when it's instanciated but I get following errors/notices:

Notice: Use of undefined constant username - assumed 'username' in C:\Users\Jinxed\Desktop\WebTrgovina\app\m\Register\User.m.php on line 7

Notice: Use of undefined constant password - assumed 'password' in C:\Users\Jinxed\Desktop\WebTrgovina\app\m\Register\User.m.php on line 7

Notice: Use of undefined constant passwordc - assumed 'passwordc' in C:\Users\Jinxed\Desktop\WebTrgovina\app\m\Register\User.m.php on line 7

... and so on for every defined variable in user class.

Here's the user class:

class User {
    function __construct(){
        $test = 'blah';
        $username; $password; $passwordc; $name; $surname; $address;
        $this->checkInput(array(username=>20, password=>20, passwordc=>20, name=>20, surname=>40, address=>40));
    }
    //array(formName=>CharacterLimit)
    private function checkInput($fields){
        foreach($fields as $field=>$limit){
            if($_POST[$field]=='' || strlen($_POST[$field])>$limit) $this->error[] = "$field must be filled in, and it must be less than or $limit characters long.";
            else $this->{$field} = $_POST[$field];
        }
    }
}

I don't quite understand what's the problem, I've tried first just creating variables and than from the main program calling checkInput method, but I get same error.

Charles
  • 50,943
  • 13
  • 104
  • 142
Jinx
  • 857
  • 2
  • 14
  • 28
  • possible duplicate of the half of Related Section to the right, for instance http://stackoverflow.com/questions/748289/php-getting-a-use-of-undefined-constant-cookie-login-how-do-i-fix-this. Please use the search function before asking duplicates. – Gordon May 01 '12 at 11:27
  • 1
    possible duplicate of http://stackoverflow.com/questions/2941169/what-does-the-php-error-message-notice-use-of-undefined-constant-mean/8025500#8025500 – Gordon May 01 '12 at 11:29

1 Answers1

8

You should be quoting the strings that you use as array keys:

$this->checkInput(array(
    'username'=>20,
    'password'=>20,
    'passwordc'=>20,
    'name'=>20,
    'surname'=>40,
    'address'=>40));

The documentation says:

If you use an undefined constant, PHP assumes that you mean the name of the constant itself, just as if you called it as a string (CONSTANT vs "CONSTANT"). An error of level E_NOTICE will be issued when this happens. See also the manual entry on why $foo[bar] is wrong (unless you first define() bar as a constant).

Jon
  • 428,835
  • 81
  • 738
  • 806
  • what do you mean? This: `$_POST["$field"]` or this `else $this->"$field" = $_POST[$field];` because I've tried this last one, and I get this error: `Parse error: syntax error, unexpected '"', expecting T_STRING or T_VARIABLE or '{' or '$' in C:\Users\Jinxed\Desktop\WebTrgovina\app\m\Register\User.m.php on line 12`. – Jinx May 01 '12 at 11:08
  • @Jinx: Finished editing, take another look. Of course you wouldn't be fixing an error that occurs on line 7 by modifying line 12. – Jon May 01 '12 at 11:09
  • Ahh.. ok thanks, I understand now. I saw couple of examples where that wasn't used. – Jinx May 01 '12 at 11:10
  • @Jinx: As soon as you gain some rep, please remember to leave comments or suggest edits to those examples so that others don't have the same problem in the future. – Jon May 01 '12 at 11:11
  • @Jinx you may notice a dollar sign (`$`) in your examples. a literal started from this sign is called "a variable" and shouldn't be quoted. While strings should be. Eventually you will learn to tell one from other. – Your Common Sense May 01 '12 at 11:34