2013-07-02 28 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」 是不需要的。