some beginner question :)

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I want to write php scripts which will be using some classes, let's
say following class aClass:

class aAclass
   public x,y;
   function __construct($a,$y)
   function appendToFile()
       fwrite($filehandler,'x='.$this->x.', y='.$this->y);

and let's look at the appendToFile() method of this class. It's using
a global variables $PATH and $FILE, which I want have defined in
'config.fphp' file (which I've included in first line). And I have two
1) What both variables $PATH, $FILE aren't available inside the class?
(when I put 'echo $PATH;' in this method I see nothing on the screen)

2) How to set some global variables wich will be available in all
files in some application without putting include in each of these
files? (I have a idea to use session but I hope that there are some
better solutions)

Re: some beginner question :)

Pawel_Iks wrote:
Quoted text here. Click to load it

You never specified them as global in the function, so the default is to
be local.

Quoted text here. Click to load it

You can't set a global variable without having to include the file which
defines the variable.

However, global variables are very bad to use.  They can be changed by
anyone, which can cause hard-to-find bugs.  If you need to use a
variable to a function, you should pass it as a parameter to the
function.  Alternatively, if it doesn't change, you should use constants
(see "define").

And BTW - instead of defining your own $PATH variable, check out
$_SERVER['DOCUMENT_ROOT'].  It points to the root directory of the web
server, and you can access your web files relative to this directory.

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Re: some beginner question :)

Quoted text here. Click to load it

To add the Jerry's reply, you might want to define a constant in
config.php instead :-

Constants have global scope by default, and would probably fit into
this scenario much better than a global variable.

If $PATH and $FILE are going to change dependent on the use of
application, then you should look at creating another function or
class that returns the correct value, but uses static information in
config.php to work the value out.

Hope this makes sense - Rob

Site Timeline