views:

43

answers:

2

PHP supports this:

$z = 5;
$str = "z is $z";  // result: "z is 5"

and it supports this:

$c = new StdClass();
$c->x = 9;
$str = "x is {$c->x}";  // result: "x is 9"

but it does NOT support this:

class abc
{
   const n = 2;
}
$str = "x is {abc::n}";  // result: "x is {abc::n}"

Why does PHP not support insertion of consts via the curly-brace syntax? Seems like it should...

A: 

Create a PHP bug then :-)

Frosty Z
This should be comment (but as the suggestion to file a feature-request is valid I do not downvote.)
nikic
+4  A: 

The curly syntax is the extended variable syntax. It is used to interpolate variables into strings. And as in PHP variables start with $ everything else will yield a syntax error.

But what you can do is call variable-functions. Thus you could do:

$_ = function ($expr) { return $expr; };

echo "Something {$_(Class::Constant)}";

But that's a hack which normally isn't appropriate. Instead please use string concatenation:

echo 'Something ' . Class::Constant;
nikic
really nice! I would consider this on php 5.3
Gabriel Sosa
Better yet: `$_ = "htmlspecialchars";` which works more universally.
mario