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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.