473,325 Members | 2,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Mimicking a global variable

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
9 1624
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: David Hitillambeau | last post by:
Hi guys, As I am new to Python, i was wondering how to declare and use global variables. Suppose i have the following structure in the same module (same file): def foo: <instructions>...
2
by: Gadrin77 | last post by:
as a newbie to XSL, is it possible to mimic a SELECT/CASE statement using XSL? I tried a quickie and I kept getting errors either using PARAM or WITH-PARAM in the wrong place or VARIABLE. I...
4
by: Andrew V. Romero | last post by:
I have been working on a function which makes it easier for me to pull variables from the URL. So far I have: <script language="JavaScript"> var variablesInUrl; var vArray = new Array(); ...
2
by: Thomas Matthews | last post by:
Hi, I'm getting linking errors when I declare a variable in the global scope, but not inside a function. The declarations are the same (only the names have been changed...). class Book {...
8
by: lawrence | last post by:
I'm learning Javascript. I downloaded a script for study. Please tell me how the variable "loop" can have scope in the first function when it is altered in the second function? It is not defined...
17
by: MLH | last post by:
A97 Topic: If there is a way to preserve the values assigned to global variables when an untrapped runtime error occurs? I don't think there is, but I thought I'd ask. During development, I'm...
9
by: Ed Jensen | last post by:
I'm having a vexing problem with global variables in Python. Please consider the following Python code: #! /usr/bin/env python def tiny(): bar = for tmp in foo: bar.append(tmp) foo = bar
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.