CodeIgniter Forums

Full Version: Constant in a class
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]sl3dg3hamm3r[/eluser]
Hey there

I wonder how I could access a constant in a class/library from outside through CI's loading mechanics:

Code:
[...]
//library lib_test.php
const SOME_CONSTANT    = 0;

Now, in a controller, this doesn't work:
Code:
$this->load->library('lib_test'));
echo $this->lib_test::CONSTANT;    //Parse-error
echo $this->lib_test->CONSTANT;    //No parse-error, but neither a result

Any suggestion how to access constants (or class-vars)?

Sl3dg3

El Forum

[eluser]roj[/eluser]
From my reading of it you'd want to use:

Code:
echo lib_test::CONSTANT

The first method you've attempted is fine in php 5.3 apparently. The second will read the object variables. You can read more on object constants in the [url-http://uk.php.net/manual/en/language.oop5.constants.php]php manual[/url]

You should also note that libraries should be capitalised...User guide

El Forum

[eluser]sl3dg3hamm3r[/eluser]
stupid, I didn't think of leaving away '$this' :rolleye: ... about the rest you wrote I'm perfectly well informed, I was just fast-typing. Thx mate.

El Forum

[eluser]roj[/eluser]
No worries, sorry if i came across patronising!