PHP中的静态函数变量和串联 [英] Static Function Variables and Concatenation in PHP

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

问题描述

请考虑以下内容:

$var = 'foo' . 'bar'; # Not a member of a class, free-standing or in a function.

但是,一旦我将$var标记为static

As soon as I mark $var as static, however:

static $var = 'foo' . 'bar';

PHP(在WAMP设置中为5.3.1)抱怨以下错误:

PHP (5.3.1 on a WAMP setup) complains with the following error:

解析错误:语法错误,意外的.",期望为,"或;"

Parse error: syntax error, unexpected '.', expecting ',' or ';'

似乎字符串连接是这里的罪魁祸首.

It seems that the string concatenation is the culprit here.

这是怎么回事?有人可以向我解释静态变量的规则吗?

What's going on here? Can someone explain the rules for static variables to me?

推荐答案

该手册在 变量范围 :

The manual states, in Variables scope:

尝试为这些[静态]变量赋值,这些变量是表达式的结果,将导致解析错误.

Trying to assign values to these [static] variables which are the result of expressions will cause a parse error.

静态关键字 中也有提及:

There is also mention of it in Static keyword:

与其他任何PHP静态变量一样,静态属性只能使用文字或常量进行初始化;不允许使用表达式.

Like any other PHP static variable, static properties may only be initialized using a literal or constant; expressions are not allowed.

尽管应该注意的是,无论是静态属性还是非静态属性,都不能使用表达式来初始化.

Although it should be noted that a property, static or not, cannot be initialized using an expression neither.

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

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