Jump to content

Problem with class static variables


r_honey

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/56567-problem-with-class-static-variables/
Share on other sites

try this way

 

<?php
class config
{
    public static $Image="/images";
    
    
}


class layout
{

   public function writeLogo()
  {
    $dir = config::$Image;  
    $text = <<<ABC
      <img class="Logo" title="My Logo" alt="Logo" src="$dir/Logo.gif" />
ABC;
     
     echo($text);
  }
}

$x = new layout;

$x->writeLogo();
?>

Obviously, that works!!!

As PHP uses a slightly different syntax & implementation for OOPs, I was searching php.net to see if there was any problem with my syntax...

 

Looks like I have to use the workaround.. Static variables are not being interpreted by the Zend engine the same as normal variables are..

 

Anyways Thanx...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.