By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,831 Members | 2,237 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,831 IT Pros & Developers. It's quick & easy.

Mimicking a global variable

P: n/a
Hello All:

I want to have some re-usable variables, which can be used inside (as
well as outside) objects. The use of GLOBALS array is one option, but
then I will have to keep track of "reserved variable names" elsewhere.

Another option is to declare a $FORALL array in an abstract class and
initialize it. And then define every object as an extension of this
abstract class.

Is there a better way to achieve this? For those, who are curious about
what I want to store ...stuff like website's description (to be used
across pages). The doctype declaration. Calculation of today's date, etc.

Best regards,
Animesh
Sep 13 '08 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Animesh K wrote:
Hello All:

I want to have some re-usable variables, which can be used inside (as
well as outside) objects. The use of GLOBALS array is one option, but
then I will have to keep track of "reserved variable names" elsewhere.

Another option is to declare a $FORALL array in an abstract class and
initialize it. And then define every object as an extension of this
abstract class.

Is there a better way to achieve this? For those, who are curious about
what I want to store ...stuff like website's description (to be used
across pages). The doctype declaration. Calculation of today's date, etc.

Best regards,
Animesh
If it's something required by the object, it should be passed to the
object, i.e. in the constructor or a setxxx() function.

Don't require the object to request information from someplace else - it
increases connections between objects and limits reusability.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Sep 14 '08 #2

P: n/a
Why not use a $_SESSION variable?

Regards
Animesh K wrote:
Hello All:

I want to have some re-usable variables, which can be used inside (as
well as outside) objects. The use of GLOBALS array is one option, but
then I will have to keep track of "reserved variable names" elsewhere.

Another option is to declare a $FORALL array in an abstract class and
initialize it. And then define every object as an extension of this
abstract class.

Is there a better way to achieve this? For those, who are curious about
what I want to store ...stuff like website's description (to be used
across pages). The doctype declaration. Calculation of today's date, etc.

Best regards,
Animesh
Sep 14 '08 #3

P: n/a
RootShell (IFReviews.org) wrote:
Animesh K wrote:
>Hello All:

I want to have some re-usable variables, which can be used inside (as
well as outside) objects. The use of GLOBALS array is one option, but
then I will have to keep track of "reserved variable names" elsewhere.

Another option is to declare a $FORALL array in an abstract class and
initialize it. And then define every object as an extension of this
abstract class.

Is there a better way to achieve this? For those, who are curious
about what I want to store ...stuff like website's description (to be
used across pages). The doctype declaration. Calculation of today's
date, etc.

Best regards,
Animesh

Why not use a $_SESSION variable?

Regards
(Please don't top post)

Same problem as he would have using the globals array - only worse. The
session array will span multiple pages.

And please don't top post. Thanks.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Sep 14 '08 #4

P: n/a
Jerry Stuckle wrote:
RootShell (IFReviews.org) wrote:
>Animesh K wrote:
>>Hello All:

I want to have some re-usable variables, which can be used inside (as
well as outside) objects. The use of GLOBALS array is one option, but
then I will have to keep track of "reserved variable names" elsewhere.

Another option is to declare a $FORALL array in an abstract class and
initialize it. And then define every object as an extension of this
abstract class.

Is there a better way to achieve this? For those, who are curious
about what I want to store ...stuff like website's description (to be
used across pages). The doctype declaration. Calculation of today's
date, etc.

Best regards,
Animesh

Why not use a $_SESSION variable?
>
Regards
>

(Please don't top post)

Same problem as he would have using the globals array - only worse. The
session array will span multiple pages.

And please don't top post. Thanks.

I think I am getting a hang of the solution you (Jerry) suggested.

Session is not the right solution since I have to store site-wide
constants in that array. A constant should just be stored in some fixed
file, rather than stored as a temporary session variable for each user.

Best regards,
Animesh

Sep 14 '08 #5

P: n/a
Animesh K wrote:
Jerry Stuckle wrote:
>RootShell (IFReviews.org) wrote:
>>Animesh K wrote:
Hello All:

I want to have some re-usable variables, which can be used inside
(as well as outside) objects. The use of GLOBALS array is one
option, but then I will have to keep track of "reserved variable
names" elsewhere.

Another option is to declare a $FORALL array in an abstract class
and initialize it. And then define every object as an extension of
this abstract class.

Is there a better way to achieve this? For those, who are curious
about what I want to store ...stuff like website's description (to
be used across pages). The doctype declaration. Calculation of
today's date, etc.

Best regards,
Animesh

Why not use a $_SESSION variable?

Regards

(Please don't top post)

Same problem as he would have using the globals array - only worse.
The session array will span multiple pages.

And please don't top post. Thanks.


I think I am getting a hang of the solution you (Jerry) suggested.

Session is not the right solution since I have to store site-wide
constants in that array. A constant should just be stored in some fixed
file, rather than stored as a temporary session variable for each user.

Best regards,
Animesh
One other comment - if you have multiple values you want to keep, often
times a configuration object is a better way to go. The object can be
passed to the functions and objects requiring information. Another
possibility (although I don't think it is as good) would be to create a
singleton object with a getInstance() method to fetch the singleton.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Sep 14 '08 #6

P: n/a
Jerry Stuckle wrote:
>
One other comment - if you have multiple values you want to keep, often
times a configuration object is a better way to go. The object can be
passed to the functions and objects requiring information. Another
possibility (although I don't think it is as good) would be to create a
singleton object with a getInstance() method to fetch the singleton.
It seems you have the solution I was hoping for. How do I set up a
configuration object? Any hints would be appreciated.

Singleton object will work too, but I think something embedded in
configuration will be great (and handy).

Best regards,
Animesh
Sep 15 '08 #7

P: n/a
Jerry Stuckle wrote:
>
It isn't that hard. Google for "singleton php" (without the quotes) and
you'll get some ideas.
This is what I am doing now. (Let me know if there is a better way to
achieve this).

globalConst.php:

class globalConst {

var $globe = array();

function __construct(){
$this->globe['description'] = 'blah blah';
$this->globe['author'] = 'Animesh Kumar';
//etc
}
}
index.php:

include('globalConst.php');
$var = new globalConst;

//new local globe
$globe = $var->globe;
unset($var);

// now $globe is available.

Sep 18 '08 #8

P: n/a
Animesh K wrote:
Jerry Stuckle wrote:
>>
It isn't that hard. Google for "singleton php" (without the quotes)
and you'll get some ideas.

This is what I am doing now. (Let me know if there is a better way to
achieve this).

globalConst.php:

class globalConst {

var $globe = array();

function __construct(){
$this->globe['description'] = 'blah blah';
$this->globe['author'] = 'Animesh Kumar';
//etc
}
}
index.php:

include('globalConst.php');
$var = new globalConst;

//new local globe
$globe = $var->globe;
unset($var);

// now $globe is available.

Yes, there are better ways. Did you google for like I suggested?
You'll find some good discussions and suggestions on how to do it.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Sep 19 '08 #9

P: n/a
Jerry Stuckle wrote:
>
Yes, there are better ways. Did you google for like I suggested? You'll
find some good discussions and suggestions on how to do it.
I did search for singleton php. Am trying to understand it. Hopefully I
will discover the better method. Thanks for pointing me in the right
direction.

Best regards,
Animesh
Sep 19 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.