Drongo_III Posted August 5, 2013 Share Posted August 5, 2013 Hi Guys I was trying to set a class property today in the same way I have for a long time but it didn't work and it made me wonder whether I have been doing something wrong for a while. When i try to set a session variable to a property of the class, as in $sessID below, i get a php error: "unexpected t_variable". I have been staring at the screen for some hours but I cant actually see anything wrong with the line. If I comment out the sessID line and instead set this property in the construct this works fine. So few questions spring to mind: 1) is there some reason you can't set a variable outside of the construct like this? 2) is it best practice to set such variables as part of the construct? 3) Why do all the other variables work fine but not the session one? It may yet transpire the answer to one of these questions is the answer to all Thanks, Drongo class user_admin extends login { public $errors = array(); private $userData = array(); private $changePassword = false; public $sessID = $_SESSION['user_logged']; public function __construct(){ //$this->sessID = $_SESSION['user_logged']; $this->getUserData(); include('/forms/user-admin.php'); } } Quote Link to comment https://forums.phpfreaks.com/topic/280846-the-right-place-to-declare-class-properties/ Share on other sites More sharing options...
Solution cyberRobot Posted August 5, 2013 Solution Share Posted August 5, 2013 From the manual (http://php.net/manual/en/language.oop5.properties.php): 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. Quote Link to comment https://forums.phpfreaks.com/topic/280846-the-right-place-to-declare-class-properties/#findComment-1443534 Share on other sites More sharing options...
Drongo_III Posted August 5, 2013 Author Share Posted August 5, 2013 (edited) From the manual (http://php.net/manual/en/language.oop5.properties.php): Sorry I completely missed that when I was reading over php.net - and now it all makes perfect sense! Thank you Edited August 5, 2013 by Drongo_III Quote Link to comment https://forums.phpfreaks.com/topic/280846-the-right-place-to-declare-class-properties/#findComment-1443535 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.