Traxus Posted July 28, 2010 Share Posted July 28, 2010 I'm confused as to why assigning these variables in the class causes the page not to load... var $RootFolder = '/shyid/'; var $PagePath = str_replace($this->RootFolder, '', dirname($_SERVER['PHP_SELF'])); var $PageSections = explode('/', $this->PagePath); but when i set them on the page, everything works correctly? $head->RootFolder = '/shyid/'; $head->PagePath = str_replace($head->RootFolder, '', dirname($_SERVER['PHP_SELF'])); $head->PageSections = explode('/', $head->PagePath); Insight? Thanks. Quote Link to comment Share on other sites More sharing options...
radar Posted July 28, 2010 Share Posted July 28, 2010 because you're using the var in php, which you don't need.. $RootFolder would suffice.... and even if it didnt but you absolutely wanted to declare them in the class file you could use: this->RootFolder = '/shyid/'; this->PagePath = str_replace(this->RootFolder, '', dirname($_SERVER['PHP_SELF'])); this->PageSelections = explode('/', this->PagePath); or something along those lines anywho... Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted July 28, 2010 Share Posted July 28, 2010 Declarations of class variables must be a constant expression, so set them in the constructor if you need default values. Also, if not using PHP 4, then use public, private or protected: class test { public $RootFolder; public $PagePath; public $PageSections; function __construct($RootFolder) { $this->RootFolder = $RootFolder; $this->PagePath = str_replace($this->RootFolder, '', dirname($_SERVER['PHP_SELF'])); $this->PageSections = explode('/', $this->PagePath); } } $test = new test('/shyid/'); Quote Link to comment Share on other sites More sharing options...
Traxus Posted July 28, 2010 Author Share Posted July 28, 2010 Declarations of class variables must be a constant expression, so set them in the constructor if you need default values. Also, if not using PHP 4, then use public, private or protected: class test { public $RootFolder; public $PagePath; public $PageSections; function __construct($RootFolder) { $this->RootFolder = $RootFolder; $this->PagePath = str_replace($this->RootFolder, '', dirname($_SERVER['PHP_SELF'])); $this->PageSections = explode('/', $this->PagePath); } } $test = new test('/shyid/'); thanks for that, still new to classes/OOP had no idea about __construct() Quote Link to comment 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.