0

CakePHP: 2.3.5

class Table extends AppModel {

    const STATUS_FREE = 0;
    const STATUS_BUSY = 1;
    const STATUS_INCHECK = 2;
    const STATUS_LINK = 3;
    const STATUS_CLEAN = 4;
    const STATUS_CASHIER = 9;

我在TablesController中,我需要访问这个常量。我为此而创立的唯一方法是:

$this->loadModel('Table');
$free = Table::STATUS_FREE;

我也试试:

self::STATUS_FREE

$this->loadModel如果我在 TablesController 中,为什么我需要使用?

4

1 回答 1

1

You dont need loadModel. You just need the class included. You can also achieve that via App::uses() - which kind of works as a lazy loaded require().

Just include the classes you need the constants from above your main class. Always.

App::uses('Table', 'Model');
App::uses('OtherModelWithContantsYouNeed', 'Model');

class TablesController extends AppController {}

Now you can use your constants anywhere in your controller code as well as all its views!

This is also how I do it for my class constants in my enums ( http://www.dereuromark.de/2010/06/24/static-enums-or-semihardcoded-attributes/ ). There is also explained in more detail what is going on.

于 2013-06-18T21:13:32.147 回答