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

Link to comment
Share on other sites

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 ;)

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.