将数组定义为类的属性时出现语法错误 [英] Syntax error while defining an array as a property of a class

查看:89
本文介绍了将数组定义为类的属性时出现语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

...

public $aSettings = array(
  'BindHost' => "127.0.0.1",
  'Port' => 9123,
  'MaxFileSize' => (5 * (1024 * 1024)), // unexpected "(" here
  'UploadedURL' => "http://localhost",
  'UploadPath' => dirname(__FILE__) . "/upload",
  'UploadMap' => dirname(__FILE__) . "/uploads.object",
  'RegisterMode' => false
);

...

这是我的代码,直接来自一个类.我遇到的问题是"unexpected ( on line 22",第22行是MaxFileSize.

This is my code, straight from a class. The problem I have is the "unexpected ( on line 22", line 22 being MaxFileSize.

我看不到问题,这是Zend Engine的限制吗?还是我瞎了?

I can't see a problem with it, is this a Zend Engine limitation? Or am I blind.

推荐答案

在5.6之前的PHP版本中初始化类属性时,不能使用非恒定值. 这些是在编译时初始化的,在编译时PHP将不进行任何计算或执行任何代码. (5 * (1024 * 1024))是需要评估的表达式,您不能在其中执行.将其替换为常量值5242880或在__construct中进行计算.

You cannot use non-constant values while initializing class properties in PHP versions earlier than 5.6.
These are initialized at compile time, at which PHP will do no calculations or execute any code. (5 * (1024 * 1024)) is an expression that requires evaluation, which you cannot do there. Either replace that with the constant value 5242880 or do the calculation in __construct.

PHP 5.6(于2014年推出)允许恒定标量表达式" ,其中标量常量或类属性可以通过类定义中的求值表达式而不是构造函数来初始化.

PHP 5.6, introduced in 2014, allows "constant scalar expressions" wherein a scalar constant or class property can be initialized by an evaluated expression in the class definition rather than the constructor.

这篇关于将数组定义为类的属性时出现语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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