php classe:从函数获取值到成员变量中时出现问题
我有一个从数据库获取值并返回它的函数。我调用函数将其存储到成员变量中,但出现以下错误:
Parse error: parse error, expecting `','' or `';'' in I:\wamp\www\deposit\classes\Site.php on line 14
这是导致错误的行
public static $depositmoney = self::get_balance();
这是从数据库获取值的函数
public static function get_balance()
{
global $link, $usertable, $userid, $useridentify;
//query current balance
$cb = mysqli_fetch_object(mysqli_query($link, "SELECT deposit FROM ".$usertable." WHERE ".$userid."=".$useridentify.""));
return $cb->deposit;
}//end of function get_balance().
所有这些代码都在同一个类中。有人知道导致错误的原因吗?
I have a function that gets a value from the database and returns it. I call the function to store it into a member variable but I get the following error:
Parse error: parse error, expecting `','' or `';'' in I:\wamp\www\deposit\classes\Site.php on line 14
This is the line that causes the error
public static $depositmoney = self::get_balance();
And this is the function that gets the value from the database
public static function get_balance()
{
global $link, $usertable, $userid, $useridentify;
//query current balance
$cb = mysqli_fetch_object(mysqli_query($link, "SELECT deposit FROM ".$usertable." WHERE ".$userid."=".$useridentify.""));
return $cb->deposit;
}//end of function get_balance().
all of this code is in the same class. Anyone an idea of what's causing the error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
类属性不能与运行时信息一起声明。
上面的方法不会起作用。
请参阅 PHP 类属性手册:(重点是我的)
您可以为
$depositmoney
创建一个 getter 并让它初始化当前未设置的值:但是,我建议摆脱
static
并使用实例方法和属性而是跟踪状态。您还需要摆脱global
内容,并通过构造函数、setter 或在方法调用期间注入依赖项。这会减少耦合并使代码更易于维护。Class properties may not be declared with run-time information.
The above will not work.
See the PHP Manual on Class Properties: (emphasis mine)
You could create a getter for
$depositmoney
and have it initialize the value if it is currently unset:However, I suggest to get rid of the
static
and use instance methods and properties instead to track the state. You will also want to get rid of theglobal
stuff and inject dependencies through the constructor, setters or during the method invocation. That decreases coupling and will make the code more maintainable.