473,386 Members | 1,846 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,386 software developers and data experts.

best practice global variables

All,
I'm coming from java and coldfusion, where one can set a "global" variable
in one place (the servletcontext in java, Application.cfm in coldfusion) and
all files in that site can then take advantage of these variables without
the need to "include" a variables page in every page on the site.
Is there something comparable in php, like an Application.php or some such
thing?

One more question that's basically an extension of the first. in
development, my site is http://localhost/mysite, but in the hosting
environment, it's http://mysite

Now, let's say I have a "header" include that includes images in it, and
that header is included in multiple directory trees, so relative pathing of
those images isn't possible. Is there a preferred way in php for dealing
with this kind of thing, since i can't simply use /images/myimage.jpg since
that path isn't valid in the local environment?

Thanks for any answers. I'm doing some emergency work on a php site as a
favor to a friend and am quite out of my element.

Marc
Jan 14 '07 #1
4 2983
Marc E wrote:
All,
I'm coming from java and coldfusion, where one can set a "global" variable
in one place (the servletcontext in java, Application.cfm in coldfusion) and
all files in that site can then take advantage of these variables without
the need to "include" a variables page in every page on the site.
Is there something comparable in php, like an Application.php or some such
thing?
Not really. But why would you need it? In several years of PHP
programming I never have - but then in general globals are not a good
thing, anyway, IMHO.
One more question that's basically an extension of the first. in
development, my site is http://localhost/mysite, but in the hosting
environment, it's http://mysite
Why isn't it http://localhost? That's what I use. Or, even
http://example (the real site being http://example.com) by using your
HOSTS file.
Now, let's say I have a "header" include that includes images in it, and
that header is included in multiple directory trees, so relative pathing of
those images isn't possible. Is there a preferred way in php for dealing
with this kind of thing, since i can't simply use /images/myimage.jpg since
that path isn't valid in the local environment?
Check out $_SERVER['DOCUMENT_ROOT'] will always point to the root
directory of your site. From there on you can use a path relative to
your document root directory - if you use the above format.
Thanks for any answers. I'm doing some emergency work on a php site as a
favor to a friend and am quite out of my element.

Marc

I'd suggest you get someone more familiar with PHP to help you. I think
you're on the wrong track here.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jan 15 '07 #2
Jerry Stuckle wrote:
Marc E wrote:
> Is there something comparable in php, like an Application.php or
some such thing?

Not really. But why would you need it? In several years of PHP
programming I never have - but then in general globals are not a good
thing, anyway, IMHO.
I follow the old Microsoft model - using INI files. I even use
the same file format, and write my own set/get routines for them.
Jan 15 '07 #3
Thanks for the quick response Jerry.
"Jerry Stuckle" <js*******@attglobal.netwrote in message
news:YJ******************************@comcast.com. ..
Marc E wrote:
>All,
I'm coming from java and coldfusion, where one can set a "global"
variable in one place (the servletcontext in java, Application.cfm in
coldfusion) and all files in that site can then take advantage of these
variables without the need to "include" a variables page in every page on
the site.
Is there something comparable in php, like an Application.php or some
such thing?

Not really. But why would you need it? In several years of PHP
programming I never have - but then in general globals are not a good
thing, anyway, IMHO.

Why need it? For the same reason that java servlets have Filters...because
it's a heck of a lot easier to be able to route all requests through
interceptors that handle specific functionality. For example, authentication
handling. i'd rather have an AuthFilter (or comparable functionality in a
coldfusion Application.cfm file) through which every request runs that
contains all the logic for ensuring a user is logged in and, if not, routed
to the appropriate page. Seems to me that if I want this type of
functionality in PHP, I have to have an include at the top of each page.
For this rinky dink site i'm doing this quick work for, it's not a big
deal..... just annoying and time wasting IMHO.
>
>One more question that's basically an extension of the first. in
development, my site is http://localhost/mysite, but in the hosting
environment, it's http://mysite

Why isn't it http://localhost? That's what I use. Or, even
http://example (the real site being http://example.com) by using your
HOSTS file.


it's not localhost because i have dozens of other sites on this machine,
with each site as a subdirectory under the root. never thought of using
hosts file before.
>
>Now, let's say I have a "header" include that includes images in it, and
that header is included in multiple directory trees, so relative pathing
of those images isn't possible. Is there a preferred way in php for
dealing with this kind of thing, since i can't simply use
/images/myimage.jpg since that path isn't valid in the local environment?

Check out $_SERVER['DOCUMENT_ROOT'] will always point to the root
directory of your site. From there on you can use a path relative to your
document root directory - if you use the above format.
that one didn't work for me. my normal doc root for all coldfusion
development is in my cfusionmx\wwwroot directory, and that's the site that's
configured as the base in IIS. but for this php stuff, i've got everything
in inetpub\wwwroot, and have virtual directories set up for it. but
Document_Root is returning the cfusionmx\wwwroot directory. good thought
though!

>
>Thanks for any answers. I'm doing some emergency work on a php site as a
favor to a friend and am quite out of my element.

Marc

I'd suggest you get someone more familiar with PHP to help you. I think
you're on the wrong track here.
Tell me about it brother. I'm so used to java and coldfusion cfcs that
coming back to regular old scripting is very foreign.

i've found php remarkably simple to learn, especially thanks to the
incredible documentation. it's just one or two things i'm coming across that
are slipping me up.

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

Jan 15 '07 #4
Marc E wrote:
Thanks for the quick response Jerry.
"Jerry Stuckle" <js*******@attglobal.netwrote in message
news:YJ******************************@comcast.com. ..
>Marc E wrote:
>>All,
I'm coming from java and coldfusion, where one can set a "global"
variable in one place (the servletcontext in java, Application.cfm in
coldfusion) and all files in that site can then take advantage of these
variables without the need to "include" a variables page in every page
on the site.
Is there something comparable in php, like an Application.php or some
such thing?

Not really. But why would you need it? In several years of PHP
programming I never have - but then in general globals are not a good
thing, anyway, IMHO.


Why need it? For the same reason that java servlets have Filters...because
it's a heck of a lot easier to be able to route all requests through
interceptors that handle specific functionality. For example,
authentication handling. i'd rather have an AuthFilter (or comparable
functionality in a coldfusion Application.cfm file) through which every
request runs that contains all the logic for ensuring a user is logged in
and, if not, routed to the appropriate page. Seems to me that if I want
this type of functionality in PHP, I have to have an include at the top of
each page. For this rinky dink site i'm doing this quick work for, it's
not a big deal..... just annoying and time wasting IMHO.
That doesn't really answer Jerry's question in the context of PHP.

BTW assuming you mean globals applying to the whole website (or application
if you prefer) you can use autoprepend to reference the file instead of
explicitly including it - but you loose a lot of transparency and
flexibility.

BUT YOU ARE NOW WRITING IN PHP. Don't load code you don't need. If you use
the autoloader PHP will find the files and load them.
>>One more question that's basically an extension of the first. in
development, my site is http://localhost/mysite, but in the hosting
environment, it's http://mysite

Check out $_SERVER['DOCUMENT_ROOT'] will always point to the root
directory of your site. From there on you can use a path relative to
your document root directory - if you use the above format.

that one didn't work for me. my normal doc root for all coldfusion
development is in my cfusionmx\wwwroot directory, and that's the site
that's configured as the base in IIS. but for this php stuff, i've got
everything in inetpub\wwwroot, and have virtual directories set up for it.
but Document_Root is returning the cfusionmx\wwwroot directory. good
thought though!
You seem to be digging yourself a hole here.

The right solution is to understand how the include_path works and set it up
for your requirements.

C.
Jan 15 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: David | last post by:
Hello. I am looking for advice on what is "best practice" regarding looping through a form to check its checkboxes and associated data fields. Here is what I am trying to do (Here is the page...
9
by: Mark Twombley | last post by:
Hi, I'm just getting back into C++ and had a question about the best practice for assigning error numbers. I have been working in VB for sometime now and there you would start assigning error...
12
by: David WOO | last post by:
Hi, I am a newbie on C++, I need to define some global variables which should be accessible to most classes. In the mean time, I don't won't the global variables be modified freely at most of...
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
17
by: Woody Splawn | last post by:
I am finding that time after time I have instances where I need to access information in a variable that is public. At the same time, the books I read say that one should not use public variables...
10
by: Jay Wolfe | last post by:
Hello, I'm trying to make sure I use best practices (and hence save myself some headaches) with the declaration and definition of global variables. Let's say I have an app with 30 files,...
5
by: csgraham74 | last post by:
Hi guys, Basically i have been developing in dotnet for a couple of years but ive had a few issues in regards to error handling. For example - I have a class that i call passing in a stored...
3
by: Nemisis | last post by:
Guys, I would like to write a error handler, or something, that will allow me to write to a database when an error occurs on my site. I am trying to implement this in the global.asax file a the...
1
by: =?Utf-8?B?bWFya203NQ==?= | last post by:
I have a simple page i'm trying to do dynmaically.. i have a page called submitcomments.aspx with the .cs codebehind.. before i created everything in design view.. now i've ripped that out and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.