Jump to content

Syntax error when creating a class


soycharliente

Recommended Posts

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

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']);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.