views:

31

answers:

2

Hi all,

I'm trying to assign a value to a static class property when defining it:

namespace Base;

abstract class Skeleton {   

protected static $entityManager = \Zend_Registry::get("EntityManager");
    ...
}

When I try to execute this code I get this error:

Parse error: syntax error, unexpected '(', expecting ',' or ';' in /var/www/
somewhere/application/models/Base/Skeleton.php on line 6

If I just assign a simple string value to it:

protected static $entityManager = "string";

Everyting is fine. Am I doing something PHP can't handle? If so, how to solve this?

+1  A: 

Class properties may not depend on data that has to be evaluated at runtime:

[Class member variables] 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.

Add a setter and set the value during bootstrap.

Gordon
+3  A: 

You can't put code that needs executing as a class variable, static or not.

Think about it, at which point does Zend_Registry::get("EntityManager") get executed, it can't be executed when the class is created because you have set it as static.

Even if it was not static, when does Zend_Registry::get("EntityManager") get run? When the object is instantiated or once? It needs to be put in a function inside the class.

jakenoble