Quantcast
Channel: PHP Freaks: PHP Help
Viewing all articles
Browse latest Browse all 13200

The right place to declare class properties

$
0
0

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

Viewing all articles
Browse latest Browse all 13200

Trending Articles