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???

    hmm... the problem seems to be that the heredoc syntax (and double quote limited strings, for that matter) recognises $Image as a variable, but regards the preceding "config::" as just text. I guess that the easiest workaround would be to just use concatenation:

    $text = '<img class="Logo" title="My Logo" alt="Logo" src="' . config::$Image . '/Logo.gif" />';

      As I said, I have been working to get it right for 2 days... And I have tried most things.

      "' . config::$Image . '/Logo.gif"

      appears in the output as "' . config:: . '/Logo.gif".

      I have even tried

      "'"  . config::$Image .  "/Logo.gif'"

      and other possible variations...

        Try setting the static variable equal to a local variable in that function and then using that local variable in your heredoc block. It's a kludge, but it should work.

          r_honey,
          What laserlight is trying to point out, is that it's the use of heredoc that is preventing it from accessing your config variable. There are two ways you can fix this, don't use heredoc

          //this
          $text = "blah blah blah";
          //insted of
          $text = <<<ABC
                           blah blah blah
                       ABC;
          

          Or assign your config variable as a variable in the method, so it can be accessed when useing heredoc.

          $cfgImage = config::$Image;
          $text = <<<ABC
          <img class="Logo" title="My Logo" alt="Logo" src="$cfgImage"/Logo.gif" />
          ABC;
          

            Yes, I also finally found out that last night....
            Because PHP uses a slightly different syntax & implementation for OOPs, I was searching php.net & debugging my code for 2 days to see if I was not using the syntax properly...

            Seems like the problem rests with parsing static variables within a heredoc...
            Anyways, thanx!!!

              Write a Reply...