当前位置: 动力学知识库 > 问答 > 编程问答 >

php - Can we pass sessions variables to the class

问题描述:

I'm trying to pass session variables into the below class. However, it doesn't appear to be working, and throws the following error:

Parse error: syntax error, unexpected T_VARIABLE in C:\wamp\www\pard_config\configuration.php on line 6

How can I solve this?

My code:

<?php

session_start();

class pardEngine{

public $hostname = $_SESSION['HOSTNAME'];

public $database = $_SESSION['DATABASE'];

public $database_user = $_SESSION['USER'];

public $database_user_pass = $_SESSION['PASSWORD'];

}

?>

网友答案:

From the Properties manual page:

Class member variables are called "properties". You may also see them referred to using other terms such as "attributes" or "fields", but for the purposes of this reference we will use "properties". They are defined by using one of the keywords public, protected, or private, followed by a normal variable declaration. 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.

Session data does not fit in the compile time category so your properties can only be set from regular code, either outside the class (since they are public) or from a method (maybe the constructor).

It's normally not practical to hard-code such values into a class code so I'd go for the constructor approach:

<?php

class pardEngine{
    private $hostname;
    private $database;
    private $database_user;
    private $database_user_pass;

    public function __construct($hostname, $database, $database_user, $database_user_pass){
        $this->hostname = $hostname;
        $this->database = $database;
        $this->database_user = $database_user;
        $this->database_user_pass = $database_user_pass;
    }
}


session_start();
$engine = new pardEngine($_SESSION['HOSTNAME'], $_SESSION['DATABASE'], $_SESSION['USER'], $_SESSION['PASSWORD']);
网友答案:

Assign these properties in the constructor of this class and not like this.

网友答案:

You cannot assign these values in the class declaration part.

Make use of constructor.

session_start();
class pardEngine{
        function __construct() {
             $hostname = $_SESSION['HOSTNAME'];
             $database = $_SESSION['DATABASE'];
             $database_user = $_SESSION['USER'];
             $database_user_pass = $_SESSION['PASSWORD'];

        }
}
网友答案:

Create a function that is able to create the object form an array:

class pardEngine
{
    public $hostname;
    public $database;
    public $database_user;
    public $database_user_pass;

    /**
     * @param array $array
     * @return partEngine
     */
    public static function createFromArray($array) {
        $obj                     = new static;
        $obj->hostname           = $array['HOSTNAME'];
        $obj->database           = $array['DATABASE'];
        $obj->database_user      = $array['USER'];
        $obj->database_user_pass = $array['PASSWORD'];

        return $obj;
    }
}

Usage:

session_start();
$engine = pardEngine::createFromArray($_SESSION);
网友答案:
session_start();
class pardEngine{
  public function __construct(){
    $hostname = $_SESSION['HOSTNAME'];
    $database = $_SESSION['DATABASE'];
    $database_user = $_SESSION['USER'];
    $database_user_pass = $_SESSION['PASSWORD'];
   }
  }
分享给朋友:
您可能感兴趣的文章:
随机阅读: