代码之家  ›  专栏  ›  技术社区  ›  Rene Terstegen

Zend和静态类属性

  •  1
  • Rene Terstegen  · 技术社区  · 14 年前

    我试图在定义静态类属性时为其赋值:

    namespace Base;
    
    abstract class Skeleton {   
    
    protected static $entityManager = \Zend_Registry::get("EntityManager");
        ...
    }
    

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

    如果我只给它分配一个简单的字符串值:

    protected static $entityManager = "string";
    

    一切都很好。我在做PHP无法处理的事情吗?如果是,如何解决?

    2 回复  |  直到 14 年前
        1
  •  3
  •   Jake N    14 年前

    你不能把需要执行的代码作为类变量,不管是静态的还是非静态的。

    想想看,在哪一点上呢 Zend_Registry::get("EntityManager") 执行,创建类时无法执行,因为您已将其设置为 static .

    即使不是静态的,什么时候呢 快跑?对象何时实例化或实例化一次?它需要放在类内部的函数中。

        2
  •  1
  •   Gordon Haim Evgi    14 年前

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

    添加一个setter并在引导过程中设置值。