0

why does this not work ?

class Test{

  private $vars = array('ALL' => 0,
                        'ONE' => 1);

  private $var = $vars['ALL']; // this does not work

  function __construct(){
    $this->var = $vars['ALL']; // this does work
  }
}

code example here: http://codepad.org/QSjHMDij

why is the array not accessible in the statement

private $var = $vars['ALL']; // this does not work

4 Answers 4

3

Probably because you can't access $this during the initialization of the class prior to the constructor getting called (which is implied when you tried to do it in the definition for $var.) Some languages (like C#) will let you do it, but I think PHP is one that will not.

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

5 Comments

but I don't use $this on line 8. (it's line 8 in the codepad example btw. here it would be line 6.)
You don't have to: it's implied. If you can't access $this, you can't access any of the member variables either. edit: Actually, it may not even be implied: I forget if it's true, but PHP may not have the same scope resolution as C++ and C#. PHP often does unusual scope resolution to avoid ambiguities, so I'd remain unsurprised.
Ok. Can I assume that the variables are only initialized when the object is initialized ?
Do you mean when the constructor is called? Yes. Moreso because PHP enforces it, though - I'd expect PHP initializes the variables in the order you write them, but that doesn't mean you can access them that way.
Just found this: "In PHP 4, only constant initializers for var variables are allowed."
0

Neither "works" in the way you intend. You are not allowed to use variables when declaring instance members (hence the unexpected T_VARIABLE error). In the constructor you are referencing a local variable named $vars which does not exist, meaning you're setting $this->var to NULL.

Access the instance member by doing $this->vars. You can only do this in the constructor.

Comments

0

When declaring members (variables), you can't assign array key values of other members, it causes a parse error.

For example, you're thinking (wrong) that $vars['ALL'] is referring to your private $var - which it is not - it also causes a parse error. There's no way for PHP to know that when you say:

private $var = $vars['ALL'];

that you're actually saying "I want value of $this->vars['ALL'] to be assigned to $this->var", at least not the way you wrote it. That's why you do that from within a function, where you can easily manipulate members, such as you did from the constructor.

You should declare members, their visibility and set some default values (like you did for $var), but you shouldn't point them to other members' values, it's simply - wrong and luckily - it doesn't work :)

Comments

0

You're trying to assign a value to a variable which is designed to be part of an object, rather than the class. What you want is static variables.

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.