1

I am using cakephp and I got an error when I use internationalization in class variable.

My class is :

class Util extends Object
{
    public static $options = array(
        'Traffic Limit' => __('Traffic Limit'),
        'Uptime Limit' => __('Uptime Limit'),
        'IP Address' => __('IP Address'),
        'MAC Address' => __('MAC Address')
    );
}

When I use internationalization in class varriable it shows error :

Error: syntax error, unexpected '(', expecting ')'

I have also try 'true' as second parameter in internationlization but same error I received.

When I use this variable with internationalization in class method

public static function getWispUserAttributeNames()
{
    $options = array(
        'Traffic Limit' => __('Traffic Limit'),
        'Uptime Limit' => __('Uptime Limit'),
        'IP Address' => __('IP Address'),
        'MAC Address' => __('MAC Address')
    );
    return $options;
}

it works perfectally.

Is there is a way to use internationalization in class variable?

2 Answers2

0
    class Util extends Object
    {
        public $options = array(
            'Traffic Limit' => __('Traffic Limit'),
            'Uptime Limit' => __('Uptime Limit'),
            'IP Address' => __('IP Address'),
            'MAC Address' => __('MAC Address')
        );
    }

I don't think you should use "static" for variable.

WordpressCoder
  • 303
  • 1
  • 7
0

You can't use complex expressions in initializers in PHP, So next expression is wrong, no matter it's static or not:

public static $options = array(
    'Traffic Limit' => __('Traffic Limit'),
    'Uptime Limit' => __('Uptime Limit'),
    'IP Address' => __('IP Address'),
    'MAC Address' => __('MAC Address')
);

but you can do a trick for static case:

After describing class:

class Util extends Object
{
    public static $options = array();
}

//do assign, at the bottom of file

Util::$options = array(
    'Traffic Limit' => __('Traffic Limit'),
    'Uptime Limit' => __('Uptime Limit'),
    'IP Address' => __('IP Address'),
    'MAC Address' => __('MAC Address')
);

See this: How to initialize static variables

Community
  • 1
  • 1
George G
  • 7,443
  • 12
  • 45
  • 59