next Posted July 13, 2008 Share Posted July 13, 2008 <?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. Link to comment https://forums.phpfreaks.com/topic/114482-assigning-a-variable-php-throws-an-error/ Share on other sites More sharing options...
rhodesa Posted July 13, 2008 Share Posted July 13, 2008 the error is because you don't have one of the required keywords: var/public/private in front of the variables. but, you are also not properly using classes. try reading up on classes: http://devzone.zend.com/node/view/id/638 Link to comment https://forums.phpfreaks.com/topic/114482-assigning-a-variable-php-throws-an-error/#findComment-588679 Share on other sites More sharing options...
next Posted July 13, 2008 Author Share Posted July 13, 2008 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. Link to comment https://forums.phpfreaks.com/topic/114482-assigning-a-variable-php-throws-an-error/#findComment-588684 Share on other sites More sharing options...
teynon Posted July 13, 2008 Share Posted July 13, 2008 I'd suggest making a constructor function and putting you're variable declarations in the constructor. class User { function User() { $this->variable="blah"; } } Link to comment https://forums.phpfreaks.com/topic/114482-assigning-a-variable-php-throws-an-error/#findComment-588688 Share on other sites More sharing options...
rhodesa Posted July 13, 2008 Share Posted July 13, 2008 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 Link to comment https://forums.phpfreaks.com/topic/114482-assigning-a-variable-php-throws-an-error/#findComment-588689 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.