Jump to content

quick question about class variables


Traxus

Recommended Posts

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

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.