PHP静态变量初始化 [英] php static variable initialization

查看:85
本文介绍了PHP静态变量初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
语法错误,同时将数组定义为类的属性

Possible Duplicate:
Syntax error while defining an array as a property of a class

我正在尝试执行以下操作:

I'm trying to do the following:

final class TestClass {
    public static $myvar = 10*10; //line 3
    //rest of code...
}

但我遇到此错误:syntax error, unexpected '*', expecting ',' or ';' [line 3]

为什么这不可能呢?当然,如果我将10 * 10更改为100,一切正常. 不允许通过数学计算来初始化静态变量吗?不可能吗?

why isn't this possible? of course, if i change 10*10 to 100, everything works ok. Is it not allowed to init a static variable with a math calculation? Not possible with any way?

推荐答案

从php文档

与其他任何PHP静态变量一样,静态属性只能使用文字或常量进行初始化;不允许使用表达式.因此,尽管您可以将静态属性初始化为整数或数组(例如),但不能将其初始化为另一个变量,函数返回值或对象.

Like any other PHP static variable, static properties may only be initialized using a literal or constant; expressions are not allowed. So while you may initialize a static property to an integer or array (for instance), you may not initialize it to another variable, to a function return value, or to an object.

这篇关于PHP静态变量初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆