在数组中使用变量

Can someone help me how to change value of an array to variable?

I would like to change from this:

class SimpleAuth
{
var $users = array(
    'admin1' => 'password1',  //
    'admin2' => 'password2',  // User 2
    'admin3' => 'password3',  // User 3
    'admin4' => 'password4',  // User 4
);
}

to:

$user = 'admin'; // value from an include file outside the class declaration
$password = 'password'; // value from an include file outside the class declaration

class SimpleAuth
{
var $users = array(
    $user => $password,       // User 1 // here is the error
    'admin2' => 'password2',  // User 2
    'admin3' => 'password3',  // User 3
    'admin4' => 'password4',  // User 4
);
}

I get 500 error. Please help! Thanks

are you looking for something like this without var. var was used to declare class member variables prior to php 5. it is still supported for backward compatibility but it only has meaning inside a class context.

$users = array(
$user => $password,       // User 1 // here is the error
'admin2' => 'password2',  // User 2
'admin3' => 'password3',  // User 3
'admin4' => 'password4',  // User 4

);

UPDATE, you can't use dynamic values when your defining the class members, as the variables will have no values when the class gets initiated. move your assignment to the __construct. In your case the construct is same as your class name.

function className() {
         $user = 'admin';
$password = 'password';

     $this->users[$user] = $password;
    }

Remove the 'var' in front of your $users variable instantiation

I just checked this code:

$user = 'admin';
$password = 'password';

$users = array(
    $user => $password,       // User 1 // here is the error
    'admin2' => 'password2',  // User 2
    'admin3' => 'password3',  // User 3
    'admin4' => 'password4',  // User 4
);

on this site (http://writecodeonline.com/php/) and it was good, remove the "var" it is not needed.