views:

959

answers:

5

Hello. I am using a PHP class for login purposes. The MySQL database that the login process checks for the user is defined like this:

    class flexibleAccess{

  var $dbName = 'mydatabase';

However, as I install the application for different people, this $dbName needs constant changes. I have decided to make a config file where I keep my database information so I would have to change there witch is more easy as this login class is "hidden" somewhere. The problem is I want to do something like this:

class flexibleAccess{

      var $dbName = $_SESSION['mydatabase'];

And I get the error: "Parse error: parse error in path/access.class.php on line 43" The line 43 is the line with $dbName... Why can't I use this to dynamically get my values from the session ? And how should I use it ?

Thanks

+2  A: 

Do it in a constructor (I'm assuming php5 here...)

class flexibleAccess{

   private $dbName;

   function __construct() {
         $this->dbName = $_SESSION['mydatabase'];
   }
}

For php4, replace __construct with the name of the class instead.

Paul Dixon
this code assumes that you db name will always come from session. for me looks like hardcoded. why no pass it as parameter n the constructor
Gabriel Sosa
The OP wanted it from the session, so that's what I demonstrated.
Paul Dixon
+1  A: 

You have to assign this variable in the constructor of the class as PHP 4 only allows constant values as initializers:

In PHP 4, only constant initializers for var variables are allowed. To initialize variables with non-constant values, you need an initialization function which is called automatically when an object is being constructed from the class. Such a function is called a constructor (see below).

Gumbo
A: 

Alternatively you can specify where the class should read this information from while calling the class, for example:

<?php
$login = new flexibleAccess();
$login->dbName = $_SESSION['mydatabase'];
?>

This way your class stays clean, and it can accept the database name (and any other information that might vary) from any source or method you prefer. As an added bonus, you don't have to pay attention to which version of PHP you're using, it'll work fine in both.

Hope this helps!

-Dave

Dave
I see that my answer got down-voted, is this method not recommended? If so, I'd love to know so I can learn from this. Thanks!
Dave
You don't put databases in sessions, you put them in configs like Gary said.
MrHus
..which has nothing to do with the given answer. I responded to the question on how to set these variables, not whether or not using sessions to save your database information was good or not.
Dave
A: 

It worked. Thank you guys !!

Manny Calavera
+4  A: 

You probably shouldn't put database configuration in the session. It feels a bit risky to me.

I would create a configuration file with the information and include it with a constant:

dbconfig.php:

constant("DBNAME", 'mydatabase');

flexibleaccess.php:

require_once 'dbconfig.php';

class flexibleAccess {
  private $dbname;

  // set the default database name to the constant DBNAME
  // but allow override
  public function __construct ( $dbname = DBNAME )
  {
    $this->dbname = $dbname;
  }
}

Any files that need database access:

require_once 'flexibleaccess.php';

$db = new flexibleaccess();

Of course, you don't need ot set it as a constant. The key is that you're putting the configuration in a separate file from your code.

Gary Richardson
Definately right; putting this information in the session is plain stupidity.
DreamWerx