公共静态变量值 [英] Public static variable value

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

问题描述

我想声明一个数组数组的公共静态变量:

I'm trying to declare a public static variable that is a array of arrays:

class Foo{
 public static $contexts = array(
    'a' => array(
      'aa'              => something('aa'),
      'bb'              => something('bb'),
    ),

    'b' => array(
      'aa'              => something('aa'),
      'bb'              => something('bb'),
    ),

  );

 // methods here

 }

 function something($s){
   return ...
 }

但我收到一个错误:


解析错误:解析错误,期望
`')''in ...

Parse error: parse error, expecting `')'' in ...


推荐答案

声明类属性时不能使用表达式。也就是说你不能调用 something()在这里,你只能使用静态值。

You can't use expressions when declaring class properties. I.e. you can't call something() here, you can only use static values. You'll have to set those values differently in code at some point.


像任何其他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.

http://www.php.net/manual/en/language.oop5.static .php

例如:

class Foo {
    public static $bar = null;

    public static function init() {
       self::$bar = array(...);
    }
}

Foo::init();

或者在 __ construct 将要实例化类。

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

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