Jump to content

Class Variable Help


iMiles

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/206573-class-variable-help/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/206573-class-variable-help/#findComment-1080489
Share on other sites

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...
   }
}

Link to comment
https://forums.phpfreaks.com/topic/206573-class-variable-help/#findComment-1080498
Share on other sites

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.