MeTitus Posted January 2, 2008 Share Posted January 2, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/84153-accessing-static-variables/ Share on other sites More sharing options...
wildteen88 Posted January 2, 2008 Share Posted January 2, 2008 You are calling your class variable incorrectly. When calling internal class variables you dont need to use the $ sign, eg: $varr = mysclass::myvar Quote Link to comment https://forums.phpfreaks.com/topic/84153-accessing-static-variables/#findComment-428376 Share on other sites More sharing options...
MeTitus Posted January 2, 2008 Author Share Posted January 2, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/84153-accessing-static-variables/#findComment-428386 Share on other sites More sharing options...
MadTechie Posted January 2, 2008 Share Posted January 2, 2008 Whats the error.. (should give to a BIG hint) something like "unexpected T_PRIVATE" or in other word using private outside of a class! Quote Link to comment https://forums.phpfreaks.com/topic/84153-accessing-static-variables/#findComment-428400 Share on other sites More sharing options...
MeTitus Posted January 2, 2008 Author Share Posted January 2, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/84153-accessing-static-variables/#findComment-428426 Share on other sites More sharing options...
MeTitus Posted January 2, 2008 Author Share Posted January 2, 2008 BTW, These private $date = date(DATE_RFC850); private $contentType = ServerConf::SUPPORTED_CONTENT_TYPE[0]; are within a class declaration... MeTitus Quote Link to comment https://forums.phpfreaks.com/topic/84153-accessing-static-variables/#findComment-428429 Share on other sites More sharing options...
MadTechie Posted January 2, 2008 Share Posted January 2, 2008 can you post a example then.. or atleast an error Quote Link to comment https://forums.phpfreaks.com/topic/84153-accessing-static-variables/#findComment-428443 Share on other sites More sharing options...
MeTitus Posted January 2, 2008 Author Share Posted January 2, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/84153-accessing-static-variables/#findComment-428484 Share on other sites More sharing options...
MadTechie Posted January 2, 2008 Share Posted January 2, 2008 the that line into a Constructor Quote Link to comment https://forums.phpfreaks.com/topic/84153-accessing-static-variables/#findComment-428497 Share on other sites More sharing options...
MeTitus Posted January 2, 2008 Author Share Posted January 2, 2008 Why is that?? Does it have to be that way? MeTitus Quote Link to comment https://forums.phpfreaks.com/topic/84153-accessing-static-variables/#findComment-428501 Share on other sites More sharing options...
MadTechie Posted January 2, 2008 Share Posted January 2, 2008 for it to work yes. simple reason is function's can't be used outside of a construct Quote Link to comment https://forums.phpfreaks.com/topic/84153-accessing-static-variables/#findComment-428507 Share on other sites More sharing options...
MeTitus Posted January 2, 2008 Author Share Posted January 2, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/84153-accessing-static-variables/#findComment-428520 Share on other sites More sharing options...
MeTitus Posted January 2, 2008 Author Share Posted January 2, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/84153-accessing-static-variables/#findComment-428718 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.