View Full Version : Problem with class static variables
r_honey
Jun 21st 2007, 9:44 am
This problem has driven me nuts in the last two days....
I have a file config.php having a class:
class config
{
public static $Image="/images";
}
Another file layout.php is as:
require_once("config.php");
class layout
{
public function writeLogo()
{
$text = <<<ABC
<img class="Logo" title="My Logo" alt="Logo" src="{config::$Image}/Logo.gif" />
ABC;
echo($text);
}
}
The problem is that {config::$Image} is evaluating to {config::}
Can anyone explain me why & what's the solution???
UnrealEd
Jun 21st 2007, 9:49 am
Don't use static, use const:
class config{
const Image = "/images";
}
r_honey
Jun 21st 2007, 9:54 am
I have already tried everything.
{config::Image} evaluates to {config::Image}!!!
krakjoe
Jun 21st 2007, 10:03 am
<?
class config
{
public static $image = "/images" ;
}
class layout
{
function __construct( )
{
printf( '<img class="Logo" title="My Logo" alt="Logo" src="%s/Logo.gif" />', config::$image );
}
}
new layout( );
?>
Curly braces won't work, use (s)printf
r_honey
Jun 21st 2007, 10:17 am
Yes, that worked...
But the sample function I provided actually outputs a large amount of code.
So, I need to concatenate the value of the variable inline.
Is that possible??? Moreover, why isn't config::$Image works???
krakjoe
Jun 21st 2007, 11:06 am
There is no real reason, and nothing wrong with your code, it just so happens that static variables contain syntax that is not interpreted by the Zend engine the same as normal variables are.
There is no difference between using sprintf and complementing functions as there is to using curly braces ....
$text = 'First text';
$two = 'Second Text';
echo "{$text} then {$two}<br />";
printf("%s then %s<br />", $text, $two );
are both exactly the same.
r_honey
Jun 21st 2007, 10:23 pm
Yeah, class static variables are not being parsed inside a heredoc properly...
Seems like I have to use workarounds... Thanx
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.