CodeIgniter Forums
Constant in a class - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Constant in a class (/showthread.php?tid=15302)



Constant in a class - El Forum - 01-31-2009

[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


Constant in a class - El Forum - 01-31-2009

[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


Constant in a class - El Forum - 01-31-2009

[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.


Constant in a class - El Forum - 01-31-2009

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