Jump to content

assigning a variable, php throws an error


next

Recommended Posts

<?php
class User {
	$user_name = trim(mysql_real_escape_string($_REQUEST['username']));
	$password = trim(md5($_REQUEST['password']));
	$keep_logged_in = isset($_REQUEST['remember']) ? true : false;
	public static $logged_in = false;
	public static $error_msg;

	public static function logIn() {
		if(isset($user_name) && isset($password)) {
			$found = Database::execute('CALL user_exists($user_name)') ? true : false

			if($found) {
				session_register($user_name);
				self::$logged_in = true;
			}
			else
				self::$error_msg = "<p id=\"error\">Sorry, user name and password don't match.</p>";

			header('location: ' . INDEX_PAGE);
			exit();

		return self::$logged_in;
		}
	}
}
?>

When i run my index page i get an error:

Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION in E:\PortableApps\WOS\www\ahk_php\lib\User.class.php on line 3

 

this is line 3:

$user_name = trim(mysql_real_escape_string($_REQUEST['username']));

i tried removing trim and real_escape, but it didn't help.

Any ideas on what might be wrong here ???

 

Thanks.

Variable declaration is still optional, so PPP is not required. Of course at first i made them private but it just gave me similar error:

<?php
class User {
	private $user_name = trim(mysql_real_escape_string($_REQUEST['username']));
	private $password = trim(md5($_REQUEST['password']));
	private $keep_logged_in = isset($_REQUEST['remember']) ? true : false;
	public static $logged_in = false;
	public static $error_msg;

	public static function logIn() {
		if(isset($user_name) && isset($password)) {
			$found = Database::execute('CALL user_exists($user_name)') ? true : false;

			if($found) {
				session_register($user_name);
				self::$logged_in = true;
			}
			else
				self::$error_msg = "<p id=\"error\">Sorry, user name and password don't match.</p>";

			header('location: ' . INDEX_PAGE);

		return self::$logged_in;
		}
	}
}
?>

gives me:

Parse error: syntax error, unexpected '(', expecting ',' or ';' in E:\PortableApps\WOS\www\ahk_php\lib\User.class.php on line 3

 

this didn't make any sense, so i removed "private".

This looks like a nice article that you reference, but i already know that, can you be a bit more specific with "improperly use classes"?

 

Thanks.

ah...you just reminded me...i don't think you can have functions in the values of the variables. put them in the class constructor instead (you don't have one of those by the way)

 

as far as not using classes properly...inside your logIn() method, you try accessing $user_name. to access the property of a class, you need to use $this->user_name

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.