CakePHP 2.3在AppModel中使用国际化时出错(CakePHP 2.3 getting error when using internationalization in AppModel)

编程入门 行业动态 更新时间:2024-10-09 21:22:40
CakePHP 2.3在AppModel中使用国际化时出错(CakePHP 2.3 getting error when using internationalization in AppModel)

我正在尝试在我的AppModel中定义一些状态常量,以便在每个模型中都可用。 要获取它们的字符串值,我想将它们存储在数组中,但是当我尝试将字符串国际化时,会导致以下错误:

致命错误:在第181行的/home/dev/www/test/lib/Cake/Utility/ClassRegistry.php中调用未定义的函数AppModel()

我的代码:

class AppModel extends Model { const STATUS_INACTIVE = 0; const STATUS_ACTIVE = 1; public $statuses = array( self::STATUS_INACTIVE => __('Inactive'), self::STATUS_ACTIVE => __('Active') ); }

我查看了核心,发现以下行引发了错误:

$instance = new $appModel($settings);

$ appModel($ settings)语句导致它,我试图调试它并得到相同的错误。 任何帮助或想法前往正确的方向来解决这个问题是值得赞赏的。

I'm trying to define some status constants in my AppModel to be available in every model. To get the string values of them I want to store them in an array, but when I try to internationalize the string it causes the following error:

Fatal error: Call to undefined function AppModel() in /home/dev/www/test/lib/Cake/Utility/ClassRegistry.php on line 181

My code:

class AppModel extends Model { const STATUS_INACTIVE = 0; const STATUS_ACTIVE = 1; public $statuses = array( self::STATUS_INACTIVE => __('Inactive'), self::STATUS_ACTIVE => __('Active') ); }

I had a look in the core and found that the following line fires up the error:

$instance = new $appModel($settings);

The $appModel($settings) statement causes it, I tried to debug it and got the same error. Any help or idea to head to the right direction to solve this is appreciated.

最满意答案

我不认为你可以在PHP中定义这样的$ status。 问题是您无法使用函数的结果定义类属性。

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

类成员变量称为“属性”。 您也可以使用“属性”或“字段”等其他术语来查看它们,但出于本参考的目的,我们将使用“属性”。 它们通过使用public,protected或private之一,然后是普通变量声明来定义。 此声明可能包括初始化,但此初始化必须是常量值 - 也就是说, 它必须能够在编译时进行评估,并且必须不依赖于运行时信息才能进行评估。

如果要使用已翻译的标签设置属性,请将其移至构造函数,该构造函数用于初始化对象;

class AppModel extends Model { const STATUS_INACTIVE = 0; const STATUS_ACTIVE = 1; public $statuses; public function __construct($id = false, $table = null, $ds = null) { parent::__construct($id, $table, $ds); $this->statuses = array( self::STATUS_INACTIVE => __('Inactive'), self::STATUS_ACTIVE => __('Active') ); } }

或者将它移动到另一个方法,这样才会在实际使用时生成,就像这样;

public function getStatusOptions() { return array( self::STATUS_INACTIVE => __('Inactive'), self::STATUS_ACTIVE => __('Active') ); }

另一个注意事项; 一个很好的技巧来定义类常量并使它们可重用,而不是将它们添加到AppModel是通过使用接口;

interface ActiveInactive { const STATUS_INACTIVE = 0; const STATUS_ACTIVE = 1; } interface FooBar { const HELLO = 'world'; } class MyModel extends AppModel implements ActiveInactive, FooBar { public function test() { echo self::STATUS_INACTIVE; // outputs '1' echo self::HELLO; // outputs 'world' } } class MyHelper extends Helper implements ActiveInactive, FooBar { public function test() { echo self::STATUS_INACTIVE; // outputs '1' echo self::HELLO; // outputs 'world' } }

这样你可以在任何地方重复使用这些常量; 在助手,模型,控制器。 你可以'结合'那些常数:)

I don't think you can define $statuses like that in PHP. The problem is that you cannot define a class property using the result of a function.

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

Class member variables are called "properties". You may also see them referred to using other terms such as "attributes" or "fields", but for the purposes of this reference we will use "properties". They are defined by using one of the keywords public, protected, or private, followed by a normal variable declaration. This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.

If you want to set the property with translated labels, move it to your constructor, which is meant for initialisation of Objects;

class AppModel extends Model { const STATUS_INACTIVE = 0; const STATUS_ACTIVE = 1; public $statuses; public function __construct($id = false, $table = null, $ds = null) { parent::__construct($id, $table, $ds); $this->statuses = array( self::STATUS_INACTIVE => __('Inactive'), self::STATUS_ACTIVE => __('Active') ); } }

Or move it to another method so that is is only generated when actually used, like this;

public function getStatusOptions() { return array( self::STATUS_INACTIVE => __('Inactive'), self::STATUS_ACTIVE => __('Active') ); }

On another note; a nice trick to define class constants and make them re-usable, without adding them to the AppModel is by using an interface;

interface ActiveInactive { const STATUS_INACTIVE = 0; const STATUS_ACTIVE = 1; } interface FooBar { const HELLO = 'world'; } class MyModel extends AppModel implements ActiveInactive, FooBar { public function test() { echo self::STATUS_INACTIVE; // outputs '1' echo self::HELLO; // outputs 'world' } } class MyHelper extends Helper implements ActiveInactive, FooBar { public function test() { echo self::STATUS_INACTIVE; // outputs '1' echo self::HELLO; // outputs 'world' } }

This way you can re-use those constants anywhere; in Helpers, Models, Controllers. And you can 'combine' those constants :)

更多推荐

本文发布于:2023-07-30 15:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1338820.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:CakePHP   AppModel   internationalization   error

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!