some beginner question :)

some beginner question :)

am 09.04.2008 12:43:47 von Pawel_Iks

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

include('config.php');
class aAclass
{
public x,y;
function __construct($a,$y) {$this->x=$x;$this->y=$y;}
function appendToFile()
{
$fileHandler=fopen($PATH.'/'.$FILE,'a');
fwrite($filehandler,'x='.$this->x.', y='.$this->y);
$fclose($fileHandler);
}
}

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
questions:
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 :)

am 09.04.2008 13:22:53 von Jerry Stuckle

Pawel_Iks wrote:
> I want to write php scripts which will be using some classes, let's
> say following class aClass:
>
> include('config.php');
> class aAclass
> {
> public x,y;
> function __construct($a,$y) {$this->x=$x;$this->y=$y;}
> function appendToFile()
> {
> $fileHandler=fopen($PATH.'/'.$FILE,'a');
> fwrite($filehandler,'x='.$this->x.', y='.$this->y);
> $fclose($fileHandler);
> }
> }
>
> 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
> questions:
> 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)
>

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

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

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.
jstucklex@attglobal.net
==================

Re: some beginner question :)

am 09.04.2008 17:10:15 von Rob

On Apr 9, 12:22=A0pm, Jerry Stuckle wrote:
> Pawel_Iks wrote:
> > I want to write php scripts which will be using some classes, let's
> > say following class aClass:
>
> > include('config.php');
> > class aAclass
> > {
> > =A0 =A0public x,y;
> > =A0 =A0function __construct($a,$y) {$this->x=3D$x;$this->y=3D$y;}
> > =A0 =A0function appendToFile()
> > =A0 =A0{
> > =A0 =A0 =A0 =A0$fileHandler=3Dfopen($PATH.'/'.$FILE,'a');
> > =A0 =A0 =A0 =A0fwrite($filehandler,'x=3D'.$this->x.', y=3D'.$this->y);
> > =A0 =A0 =A0 =A0$fclose($fileHandler);
> > =A0 =A0}
> > }
>
> > 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
> > questions:
> > 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)
>
> You never specified them as global in the function, so the default is to
> be local.
>
> > 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)
>
> 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. =A0They can be changed by
> anyone, which can cause hard-to-find bugs. =A0If you need to use a
> variable to a function, you should pass it as a parameter to the
> function. =A0Alternatively, 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']. =A0It 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.
> jstuck...@attglobal.net
> ==================- Hide quoted text -=

>
> - Show quoted text -

To add the Jerry's reply, you might want to define a constant in
config.php instead :-
http://uk.php.net/manual/en/language.constants.php

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