Jump to content

Accessing Static Variables


MeTitus

Recommended Posts

Hi guys,

 

I've two defined classes:

 


class ServerConf
{
    public static $QUERY_WORD = "args";
    public static $CACHE_CONTROL = "no-cache, must-revalidate";
    public static $SERVER = "someServer";
    public static $CHARSET = "iso-8859-1";
}

class HTTPHeaders
{
    private $server = ServerConf::$SERVER; --> Here
    private $charset = ServerConf::$CHARSET;
    private $date = date(DATE_RFC850);
    private $contentType = ServerConf::$SUPPORTED_CONTENT_TYPE[0];
    private $contentLength = "";
    private $cacheControl = ServerConf::$CACHE_CONTROL;
}

 

and I am getting the following error:

 

"Parse error: syntax error, unexpected T_VARIABLE, expecting T_STRING"

in the marked lined.

 

What is the problem?

 

Thanks,

 

MeTitus

Link to comment
https://forums.phpfreaks.com/topic/84153-accessing-static-variables/
Share on other sites

Hi wildteen88,

 

Thanks for your reply. I am also having a problem while trying to assign this class members, what is the problem in here.

 


class ServerConf
{
    public static $QUERY_WORD = "args";
    public static $CACHE_CONTROL = "no-cache, must-revalidate";
    public static $SERVER = "someServer";
    public static $CHARSET = "iso-8859-1";

    public static $SUPPORTED_CONTENT_TYPE = array
        (
            "text/plain",
            "text/xml",
            "text/html",
            "image/png",
            "image/jpeg"
        );
}

    private $date = date(DATE_RFC850);
    private $contentType = ServerConf::SUPPORTED_CONTENT_TYPE[0];

 

Once again, many thanks.

 

MeTitus

Hi MadTechie,

 

thanks for your reply. Bellow is the error I am getting for both instruction.

 

"Parse error: syntax error, unexpected '(', expecting ',' or ';' "

 

 

MeTitus

 

"or in other word using private outside of a class!" --> Not in my case, I'm sure ;)

 

Fair enough...

 

Here is the class definition:

 

class HTTPHeaders
{
    ///////////////////////////////
    ///    INSTANCE MEMBERS     ///
    ///////////////////////////////

    private $server = ServerConf::SERVER;
    private $charset = ServerConf::CHARSET;
    private $date = date("D M j G:i:s T Y");
    //private $contentType = ServerConf::SUPPORTED_CONTENT_TYPE[0];
    private $contentLength = "";
    private $cacheControl = ServerConf::CACHE_CONTROL;


    ///////////////////////////////
    ///    INSTANCE METHODS     ///
    ///////////////////////////////

    public function setServer($pServer)
    {
        $this->server = $pServer;
    }

    ///////////////////////////////

    public function setCharset($pCharset)
    {
        $this->charset = $pCharset;
    }

    ///////////////////////////////

    public function setContentType($pContentType)
    {
        $this->contentType = $pContentType;
    }

    ///////////////////////////////
    
    public function setContentLength($pContentLength)
    {
        $this->contentLength = $pContentLength;
    }

    ///////////////////////////////

    public function sendHeaders()
    {
        header("Server:" . $this->server);
        header("Charset:" . $this->charset);
        header("Content-Length: " . $this->contentLength);
        header("Content-Type:" . $this->contentType);
        header("Cache-Control:" . $this->cacheControl);
    }


    ///////////////////////////////
    ///      CLASS METHODS      ///
    ///////////////////////////////

    public static function sendDefaultHeaders($pLength)
    {
        header("Date:" . date(DATE_RFC850));
        header("Server:" . ServerConf::SERVER);
        header("Charset:" . ServerConf::CHARSET);
        header("Content-Length: " . $pLength);
        header("Content-Type:" . ServerConf::SUPPORTED_CONTENT_TYPE[0]);
        header("Cache-Control:" . ServerConf::CACHE_CONTROL);
    }

    ///////////////////////////////
}

 

and this is the error I'm getting:

 

"Parse error: syntax error, unexpected '(', expecting ',' or ';'" --->  private $date = date("D M j G:i:s T Y");

 

MeTitus

 

for it to work yes.

 

simple reason is function's can't be used outside of a construct

 

Is it a php limitation or a bug?

 

The other error I am having is this:

 

"Parse error: syntax error, unexpected '['"

 

on this line:

 

$this->contentType = ServerConf::SUPPORTED_CONTENT_TYPE[0];

 

I really appreciate the time you're having to help with this thanks.

 

MeTitus

You are calling your class variable incorrectly. When calling internal class variables you dont need to use the $ sign, eg:

$varr = mysclass::myvar

 

I've replaced this:

 

    function __construct()
    {
        $this->server = ServerConf::SERVER;
        $this->charset = ServerConf::CHARSET;
        $this->date = date(DATE_RFC850);
        $this->cacheControl = ServerConf::CACHE_CONTROL;
    }

 

with this:

 

    function __construct()
    {
        $this->server = ServerConf::$SERVER;
        $this->charset = ServerConf::$CHARSET;
        $this->date = date(DATE_RFC850);
        $this->contentType = ServerConf::SUPPORTED_CONTENT_TYPE[0];
        $this->cacheControl = ServerConf::$CACHE_CONTROL;
    }

 

If the members on ServerConf were declared as const, that would work as I've tried.

 

MeTitus

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.