Class constant based on other class constants

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
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Class constant based on other class constants

Post by anjanesh »

Using PHP 5.2.1 :

Code: Select all

class cls
 {
        const a = 1;
        const b = 2;
        const c = 4;
        const x = self::a + self::b + self::c; // Error
 }
I get

Code: Select all

Parse error: syntax error, unexpected '+', expecting ',' or ';' in xxx.php on line [i]nn[/i]
Is a way to create a class-constant based on other class-constants ?

Thanks
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

No, you cannot use expressions in constants.
See viewtopic.php?t=65510
Post Reply