Connecting Tech Pros Worldwide Forums | Help | Site Map

best practice global variables

Marc E
Guest
 
Posts: n/a
#1: Jan 14 '07
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



Jerry Stuckle
Guest
 
Posts: n/a
#2: Jan 15 '07

re: best practice global variables


Marc E wrote:
Quote:
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.
Quote:
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.
Quote:
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.
Quote:
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.
jstucklex@attglobal.net
==================
Sanders Kaufman
Guest
 
Posts: n/a
#3: Jan 15 '07

re: best practice global variables


Jerry Stuckle wrote:
Quote:
Marc E wrote:
Quote:
Quote:
> 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.
Marc E
Guest
 
Posts: n/a
#4: Jan 15 '07

re: best practice global variables


Thanks for the quick response Jerry.


"Jerry Stuckle" <jstucklex@attglobal.netwrote in message
news:YJ-dnUVjBbD_WjfYnZ2dnUVZ_sOknZ2d@comcast.com...
Quote:
Marc E wrote:
Quote:
>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.


Quote:
>
Quote:
>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.


Quote:
>
Quote:
>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!



Quote:
>
Quote:
>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.

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

Colin McKinnon
Guest
 
Posts: n/a
#5: Jan 15 '07

re: best practice global variables


Marc E wrote:
Quote:
Thanks for the quick response Jerry.
>
>
"Jerry Stuckle" <jstucklex@attglobal.netwrote in message
news:YJ-dnUVjBbD_WjfYnZ2dnUVZ_sOknZ2d@comcast.com...
Quote:
>Marc E wrote:
Quote:
>>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.
Quote:
Quote:
Quote:
>>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.
Closed Thread