soycharliente Posted April 21, 2010 Share Posted April 21, 2010 I've never made a class in PHP before. Trying to learn how. I'm getting a parse error and I don't know why. Parse error: syntax error, unexpected T_VARIABLE in /class.login.php on line 7 <html> <body> <?php /* LOGIN CLASS */ class Login { public $username = $_POST['Username']; /* LINE 7 */ public $password = $_POST['Password']; } if (isset($_POST['Submit']) && $_POST['Submit']=='Submit') { $login = new Login(); echo 'Username: '.$username.'<br/>'.'Password: '.$password; } ?> <form action="login.php" method="post"> <p> UN: <input type="text" name="Username" value="" /> <br /> PW: <input type="text" name="Password" value="" /> </p> <p><input type="submit" name="Submit" value="Submit" /></p> </form> </body> </html> Link to comment https://forums.phpfreaks.com/topic/199288-syntax-error-when-creating-a-class/ Share on other sites More sharing options...
PFMaBiSmAd Posted April 21, 2010 Share Posted April 21, 2010 You can only assign fixed values (strings, empty array...) when you define a class variable. You cannot use a variable on the right-hand side in the definition. Link to comment https://forums.phpfreaks.com/topic/199288-syntax-error-when-creating-a-class/#findComment-1046000 Share on other sites More sharing options...
Philip Posted April 21, 2010 Share Posted April 21, 2010 PFMaBiSmAd is correct. Instead you could pass them via a construct parameter... something like: class Login { public $username, $password; public function __construct($user, $pass) { $this->username = $user; $this->password = $pass; } } $l = new Login($_POST['user'], $_POST['pass']); Link to comment https://forums.phpfreaks.com/topic/199288-syntax-error-when-creating-a-class/#findComment-1046022 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.