David Šili
David Šili

Reputation: 117

Accessing Codeigniter model constants from outside the model

I am working on a Codeigniter project and would like to start using constants. I know that Codeigniter supports global constants and also I've managed to implement it in a scope of a model.

In the beginning of the "Baz" model:

const FOO = 'bar';

Somewhere inside the model:

echo self::FOO; // prints 'bar'; - OK

However, when I try to call it from outside the model (from the controller or some other model):

echo $this->Baz::FOO;

Even though the "Baz" model was properly loaded few lines before, it throws me an error:

... syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM) ...

I know that there is an option of using global constants, but IMO it does not follow the move away from the globally defined variables and constants in cases when they are supposed to be used only for specific models and rarely outside of them. That is why I am wondering if there is any other way to access those model constants from outside the model? Thanks

Upvotes: 1

Views: 1313

Answers (1)

sulaiman sudirman
sulaiman sudirman

Reputation: 1845

Let's say your model class name is Baz_model, you can access your constants like following;

$this->load->model('Baz_model');

echo Baz_model::FOO;

Upvotes: 0

Related Questions