0

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

5
  • can you show the error? the error from the php error log Commented Jul 2, 2013 at 15:18
  • 3
    The only error I'm getting is because of the var. Commented Jul 2, 2013 at 15:19
  • Take a look at your server error log - that should tell you what's gone wrong. Commented Jul 2, 2013 at 15:20
  • The error is in the line before. See: codepad.org/xG5rrz9z Commented Jul 2, 2013 at 15:22
  • I have updated my question as I didn't know the class declaration makes a difference. Commented Jul 2, 2013 at 15:42

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

7 Comments

Yes, it's inside a class declaration
So I just put your code right before the class declaration? Sorry, I am a newbie :p I have just purchased these code from codecanyon for login authentication.
your class should have the class constructor, which is the initial function that gets called when you initiate the class. Look for a function inside the class which has the same name as the class name, if not look for a function named '__construct' in your class. The code i gave you needs to go inside that function.
Thanks! working on it but doesn't seems easy coz it has lots of functions inside the class. There is no function called '__construct' and neither has function SimpleAuth()
ok create a function called SimpleAuth and place it in there.
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.