Constant concatenation

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
pascalz
Forum Newbie
Posts: 3
Joined: Wed Dec 17, 2003 1:46 am
Location: France
Contact:

Constant concatenation

Post by pascalz »

if I make this :

Code: Select all

define("MYCONST","MYCONSTVALUE",TRUE);
and in a class I declare a variable like this :

Code: Select all

var $MyVar = MYCONST." something";
I have an error message !
Parse error: parse error, unexpected '.', expecting ',' or ';' in ....

Constants seems dont work in concatenation...
if I do :

Code: Select all

var $MyVar = MYCONST; // It's working !
Is there a way to do this ?
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Code: Select all

<?php
$const = MYCONST;
$MyVar = $const." something";
?>
How is that?
User avatar
pascalz
Forum Newbie
Posts: 3
Joined: Wed Dec 17, 2003 1:46 am
Location: France
Contact:

Post by pascalz »

Sami wrote:

Code: Select all

<?php
$const = MYCONST;
$MyVar = $const." something";
?>
How is that?
I'm in a class, i try this :

Code: Select all

var $const = MYCONST;
var $MyVar = $this->const." something";
I've now:
Parse error: parse error, unexpected T_VARIABLE in ...
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

heh, my brain isn't functioning right, right now so I'll get back to you on that later today if nobody else helps you by then. Image
User avatar
aquila125
Forum Commoner
Posts: 96
Joined: Tue Dec 09, 2003 10:39 am
Location: Belgium

Post by aquila125 »

try removing the $this-> if you'r not in a method, I don't think you need it...
User avatar
pascalz
Forum Newbie
Posts: 3
Joined: Wed Dec 17, 2003 1:46 am
Location: France
Contact:

Post by pascalz »

aquila125 wrote:try removing the $this-> if you'r not in a method, I don't think you need it...
I've the same error :?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

You can't have non-constant initializers in class. Seems like all expressions are treated as non-constant in PHP. So move the assignment to constructor.
Post Reply