iMiles Posted July 2, 2010 Share Posted July 2, 2010 Hey there, so I have a class: <?php session_start(); class user { var $id = $_SESSION['id']; var $username = $_SESSION['username']; var $firstname = $_SESSION['firstname']; var $lastname = $_SESSION['lastname']; } ?> When I run the page, I get this error: Parse error: syntax error, unexpected T_VARIABLE in /Users/home/Desktop/Miles/HS/classes.php on line 7 Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/206573-class-variable-help/ Share on other sites More sharing options...
PFMaBiSmAd Posted July 2, 2010 Share Posted July 2, 2010 The var declaration can only use constant values (literal strings, numbers, or a static array) - This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated. You would need to use code in the constructor or in a class method to assign values from variables to the class variables. Quote Link to comment https://forums.phpfreaks.com/topic/206573-class-variable-help/#findComment-1080489 Share on other sites More sharing options...
iMiles Posted July 2, 2010 Author Share Posted July 2, 2010 So I removed the "Var", but it still doesn't work. What should I do? Quote Link to comment https://forums.phpfreaks.com/topic/206573-class-variable-help/#findComment-1080491 Share on other sites More sharing options...
AbraCadaver Posted July 2, 2010 Share Posted July 2, 2010 You can use var, but better public, private or protected. But if it is not a constant value (can't be another variable), then do as PFMaBiSmAd said and assign in the constructor: class user { var $id; function __construct() { $this->id = $_SESSION['id']; // etc... } } Quote Link to comment https://forums.phpfreaks.com/topic/206573-class-variable-help/#findComment-1080498 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.