1

i have this code:
protected $val = Zend_Registry::get('values');

Whenever I put this piece of code I get:
Parse error: syntax error, unexpected '(', expecting ',' or ';' in ...

Why is it happening?

3 Answers 3

4

You cannot use a function call or other dynamic expression to initialize a class property. It can only be a constant or atomic value. If you need to initialize it with a function call, you must do this instead inside the constructor.

protected $val = NULL;

public function __construct() {
  $this->val = Zend_Registry::get('values');
}

From the docs:

This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.

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

Comments

3

You can not use the return-value of a function for the initial value of a class-variable.

You can however set it in the constructor of the class.

class Myclass{
     protected $val;

     public function __construct(){
          $this->val = Zend_Registry::get('values');
     }
}

Comments

0

Because that looks like a class variable and you cant assign data to a class variable like that.

See here http://www.php.net/manual/en/language.oop5.properties.php

You could do it like this.

class something {
    protected $_val;

    public function __construct()
    {
        $this->_val = Zend_Registry::get('values');
    }
}

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.