Class Const Instantiation...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
BETA
Forum Commoner
Posts: 47
Joined: Fri Jul 25, 2008 3:21 am

Class Const Instantiation...

Post 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!!
mickeyunderscore
Forum Contributor
Posts: 129
Joined: Sat Jan 31, 2009 9:00 am
Location: UK

Re: Class Const Instantiation...

Post 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?
BETA
Forum Commoner
Posts: 47
Joined: Fri Jul 25, 2008 3:21 am

Re: Class Const Instantiation...

Post 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!
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Class Const Instantiation...

Post 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).
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Class Const Instantiation...

Post 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.
mickeyunderscore
Forum Contributor
Posts: 129
Joined: Sat Jan 31, 2009 9:00 am
Location: UK

Re: Class Const Instantiation...

Post 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.
BETA
Forum Commoner
Posts: 47
Joined: Fri Jul 25, 2008 3:21 am

Re: Class Const Instantiation...

Post 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
Post Reply