Page 1 of 1

Class Const Instantiation...

Posted: Fri Feb 06, 2009 9:30 am
by BETA
Hello again devnet users!
I'm starting to use classes in PHP and im trying to get used to them...
they seem not that hard and they look awesome but i found something i don't know if it's even possible or how to go about it...
Well the thing is let's say i have a constant declared in the class and i want to be able to instantiate that class. Nice, but now when i instantiate it and try to echo the const it shows nothing... So what did i do? i tried to __construct it like:

Code: Select all

function __construct(){
$this->CONST = self::CONST;
}
but then we have a problem: The const is not a constant anymore... it can be freely modified in the instance...
SO, my question: Is there any way I can get my const instantiated and be able to show it without modifying it?? Or Am I doing something wrong??

Thx in advance!!

Re: Class Const Instantiation...

Posted: Fri Feb 06, 2009 9:44 am
by mickeyunderscore
Constants do not need to be referenced using $this->, they require the class operator ::

e.g.:

Code: Select all

class ConstantClass{
    const SOMETHING = 500;
 
    public function getConstant(){
        return self::SOMETHING;
    }
}
echo ConstantClass::SOMETHING;
$const = new ConstantClass();
echo $const->getConstant();
echo $const::SOMETHING; //not sure, see note
As you can see they are available in a class context, before object instantiation, and if you want to refer to them within the class, you must use self::, rather than $this->

Note: I think the $const::SOMETHING syntax only works in more recent versions of PHP, as it doesn't work on my XAMPP installation at the moment, but I'm sure I've used it before!! Can someone confirm this?

Re: Class Const Instantiation...

Posted: Fri Feb 06, 2009 9:48 am
by BETA
the thing is i tried to use $const::SOMETHING and didn't work in my WAMP server neither so that's why i thought it didn't work... i didn't even think it was a PHP version issue...
THX for the info!

Re: Class Const Instantiation...

Posted: Fri Feb 06, 2009 11:02 pm
by Weirdan
mickeyunderscore wrote:Note: I think the $const::SOMETHING syntax only works in more recent versions of PHP, as it doesn't work on my XAMPP installation at the moment, but I'm sure I've used it before!! Can someone confirm this?
Doesn't work in 5.2.8 (current stable).

Re: Class Const Instantiation...

Posted: Sat Feb 07, 2009 12:32 am
by Chris Corbyn
Weirdan wrote:
mickeyunderscore wrote:Note: I think the $const::SOMETHING syntax only works in more recent versions of PHP, as it doesn't work on my XAMPP installation at the moment, but I'm sure I've used it before!! Can someone confirm this?
Doesn't work in 5.2.8 (current stable).
I don't think that'll ever work. Class constants are attributes on the class, not the instance.

Re: Class Const Instantiation...

Posted: Sat Feb 07, 2009 5:38 am
by mickeyunderscore
Thanks for confirming, that actually makes sense to me now that I think about it. There would be no need to access a constant for an invididual instance, as it's a constant and will never change from the class definition, I think I was a bit tired when I wrote that reply originally, lol.

Re: Class Const Instantiation...

Posted: Sat Feb 07, 2009 5:45 am
by BETA
well that makes sense to me...
I just wanted to be able to show the constants in the instance...
So I just created an Info() fuction displaying the constants.
Thx to u all for ur help!! :D