Problem with class static variables

Discussion in 'PHP' started by r_honey, Jun 21, 2007.

  1. #1
    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???
     
    r_honey, Jun 21, 2007 IP
  2. UnrealEd

    UnrealEd Peon

    Messages:
    148
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Don't use static, use const:
    class config{
      const Image = "/images";
    }
    PHP:
     
    UnrealEd, Jun 21, 2007 IP
  3. r_honey

    r_honey Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I have already tried everything.

    {config::Image} evaluates to {config::Image}!!!
     
    r_honey, Jun 21, 2007 IP
  4. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #4
    
    <?
    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( );
    ?>
    
    PHP:
    Curly braces won't work, use (s)printf
     
    krakjoe, Jun 21, 2007 IP
  5. r_honey

    r_honey Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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???
     
    r_honey, Jun 21, 2007 IP
  6. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #6
    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 );
    
    PHP:
    are both exactly the same.
     
    krakjoe, Jun 21, 2007 IP
  7. r_honey

    r_honey Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Yeah, class static variables are not being parsed inside a heredoc properly...

    Seems like I have to use workarounds... Thanx
     
    r_honey, Jun 21, 2007 IP