English 中文(简体)
Zend and static class properties
原标题:

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

namespace Base;

abstract class Skeleton {   

protected static $entityManager = end_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?

最佳回答

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.

问题回答

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.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签