2013-07-02 29 views
0

有人可以帮助我如何将数组的值更改为变量?在数组中使用变量

我想从:

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

到:

$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 
); 
} 

我得到500错误。请帮忙!由于

+0

你能显示错误吗?来自php错误日志的错误 – DevZer0

+3

我得到的唯一错误是因为'var'。 – andrewsi

+0

看看你的服务器错误日志 - 应该告诉你出了什么问题。 – 2013-07-02 15:20:38

回答

1

前删除“变种”你在找这样的事情没有varvar用于在php 5之前声明类成员变量。它仍支持向后兼容性,但它只在类上下文中有意义。

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

);

UPDATE,当定义类成员时不能使用动态值,因为当类启动时变量将没有值。将您的任务移至__construct。在你的情况下,这个结构和你的类名相同。

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

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

是的,它是在一个类声明 –

+0

看看我更新的答案 – DevZer0

+0

所以我只是把你的代码正确的类声明之前?对不起,我是新手:p我刚刚从codecanyon购买了这些代码进行登录验证。 –

0

在$用户变量实例

2

我只是检查验证码:本网站上(http://writecodeonline.com/php/

$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 
); 

,这是很好的,去掉 “VAR” 是不需要的。