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

include() in separate namespace?

P: n/a
I would like to make use of two separate PHP applications which happen
to share some class names; trying to include both from one script
results in the expected error, "cannot redefine class foo." or somesuch.

Using a fully qualified URL instead of the local file path in the
include() would work, except that the applications then do not have
access to the users cookies -- the requests are coming from the server
itself, not the user.

The virtual() function seems to be in flux -- there seems to be
disagreement as to whether it is supposed to work with .php files;
besides it doesn't work on the two servers I have.

So, is there a way to include a .php file such that it is evaluated
outside of the context of the php script from which it is included, in
its own namespace, but so that the request for the file still comes from
the user's browser itself?

I don't want to use frames for UI reasons. Thanks for any advice,

Nathan
Jul 16 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Nathan Lamont wrote:
I would like to make use of two separate PHP applications which happen
to share some class names; trying to include both from one script
results in the expected error, "cannot redefine class foo." or somesuch.
Just use

include_once('classfile.php');

or
require_once('classfile.php');

(it's better to use require() if you *always* want the file to be "included"
- you may improve speed a little by using include() for code that is
conditionally included or where you specify the filename:

e.g.

include('includefiles/'.$myclassname.'.php');
So, is there a way to include a .php file such that it is evaluated
outside of the context of the php script from which it is included, in
its own namespace, but so that the request for the file still comes from
the user's browser itself?


You can do things like
include('http://example.com/dynamic.php');
but the request will come from the webserver, not from the user's browser.

I don't quite understand what you're trying to do here - if you could explain
better (use words, don't post your code!) it might be easy for people to help
you

Matt
Jul 16 '05 #2

P: n/a
In article <a_*****************@wards.force9.net>,
matty <ma*******@askmenoquestions.co.uk> wrote:

[snip]
You can do things like
include('http://example.com/dynamic.php');
but the request will come from the webserver, not from the user's browser.

I don't quite understand what you're trying to do here - if you could explain
better (use words, don't post your code!) it might be easy for people to help
you

Matt


Thanks for responding. Let me illustrate:

Suppose I have two php scripts, foo.php and bar.php, each of which
happens to use its own separate definition of a class, say myClass. In
foo.php, myClass does one thing and in bar.php it does something
entirely different. Both foo.php and bar.php rely on data stored in the
end-user's cookies.

If I want to make use of the output of both foo.php and bar.php in a
target script, say main.php, I can't simply include() a local path to
both foo.php and bar.php, because their class names collide.

If, in main.php, I use a full url in the include() statement, foo.php
and bar.php can't perform their full functions, which rely on cookies,
since as we both pointed out the request will actually be coming from
the computer serving up main.php.

In reality, foo.php and bar.php are large multi-file php applications
that may need to be independently updated; modifying them is not an
option.

I don't want to use frames for UI reasons. So, my question is again, is
there a way to include a .php file such that it is evaluated
outside of the context of the php script from which it is included, in
its own namespace, but so that the request for the file still comes from
the user's browser itself?

I haven't been able to find a way to do so; is there another solution to
my problem?

Thanks,

Nathan
Jul 16 '05 #3

P: n/a
Nathan Lamont wrote:
In article <a_*****************@wards.force9.net>,
matty <ma*******@askmenoquestions.co.uk> wrote:

[snip]
You can do things like
include('http://example.com/dynamic.php');
but the request will come from the webserver, not from the user's
browser.

I don't quite understand what you're trying to do here - if you could
explain better (use words, don't post your code!) it might be easy for
people to help you

Matt


Thanks for responding. Let me illustrate:

Suppose I have two php scripts, foo.php and bar.php, each of which
happens to use its own separate definition of a class, say myClass. In
foo.php, myClass does one thing and in bar.php it does something
entirely different. Both foo.php and bar.php rely on data stored in the
end-user's cookies.

If I want to make use of the output of both foo.php and bar.php in a
target script, say main.php, I can't simply include() a local path to
both foo.php and bar.php, because their class names collide.

If, in main.php, I use a full url in the include() statement, foo.php
and bar.php can't perform their full functions, which rely on cookies,
since as we both pointed out the request will actually be coming from
the computer serving up main.php.

In reality, foo.php and bar.php are large multi-file php applications
that may need to be independently updated; modifying them is not an
option.

I don't want to use frames for UI reasons. So, my question is again, is
there a way to include a .php file such that it is evaluated
outside of the context of the php script from which it is included, in
its own namespace, but so that the request for the file still comes from
the user's browser itself?

I haven't been able to find a way to do so; is there another solution to
my problem?


OK, you could use cURL, or something like Pear's HTTP::Request
(http://pear.php.net/) to fetch the output of the scripts as a http request,
BUT here's the important bit:

when you fetch the page, do it as an HTTP proxy, so pass any and all http
headers along (like user-agent, http-referer, etc) and send remote-addr
and x-forwarded-for headers to match the user's ip address

That way, the webserver at the other end thinks you're a proxy server, and then
everything works ok.

If you just want to include the files locally, you'll have to change the class
names - that's how it works. It may not be too bad, if you're just redfining the
main boundary class, but it could well be a big task!

hth

Jul 16 '05 #4

P: n/a
Nathan Lamont:
Suppose I have two php scripts, foo.php and bar.php, each of which
happens to use its own separate definition of a class, say myClass. In
foo.php, myClass does one thing and in bar.php it does something
entirely different. Both foo.php and bar.php rely on data stored in the
end-user's cookies.

If I want to make use of the output of both foo.php and bar.php in a
target script, say main.php, I can't simply include() a local path to
both foo.php and bar.php, because their class names collide.

If, in main.php, I use a full url in the include() statement, foo.php
and bar.php can't perform their full functions, which rely on cookies,
since as we both pointed out the request will actually be coming from
the computer serving up main.php.

In reality, foo.php and bar.php are large multi-file php applications
that may need to be independently updated; modifying them is not an
option.

I don't want to use frames for UI reasons. So, my question is again, is
there a way to include a .php file such that it is evaluated
outside of the context of the php script from which it is included, in
its own namespace, but so that the request for the file still comes from
the user's browser itself?

I haven't been able to find a way to do so; is there another solution to
my problem?


In short no, because PHP doesn't have namespaces. To me it sounds like you
need to choose one of the classes and execute it's code by making a
separate http request, forwarding the cookie data to this request, and then
include the result. Maybe curl can help: http://www.php.net/curl

André Nęss
Jul 16 '05 #5

P: n/a
In article <Dp*****************@wards.force9.net>,
matty <ma*******@askmenoquestions.co.uk> wrote:
OK, you could use cURL, or something like Pear's HTTP::Request
(http://pear.php.net/) to fetch the output of the scripts as a http request,
BUT here's the important bit:

when you fetch the page, do it as an HTTP proxy, so pass any and all http
headers along (like user-agent, http-referer, etc) and send remote-addr
and x-forwarded-for headers to match the user's ip address

That way, the webserver at the other end thinks you're a proxy server, and
then
everything works ok.

If you just want to include the files locally, you'll have to change the
class
names - that's how it works. It may not be too bad, if you're just redfining
the
main boundary class, but it could well be a big task!


OK -- thanks for the pointers, I appreciate it (thanks to Andre Naess
too). Your curl approach looks good and I wouldn't have thought of it.

Thanks again,

Nathan
Jul 16 '05 #6

P: n/a


Suppose I have two php scripts, foo.php and bar.php, each of which
happens to use its own separate definition of a class, say myClass. In
foo.php, myClass does one thing and in bar.php it does something
entirely different. Both foo.php and bar.php rely on data stored in the
end-user's cookies.

If I want to make use of the output of both foo.php and bar.php in a
target script, say main.php, I can't simply include() a local path to
both foo.php and bar.php, because their class names collide.

If, in main.php, I use a full url in the include() statement, foo.php
and bar.php can't perform their full functions, which rely on cookies,
since as we both pointed out the request will actually be coming from
the computer serving up main.php.

In reality, foo.php and bar.php are large multi-file php applications
that may need to be independently updated; modifying them is not an
option.

I don't want to use frames for UI reasons. So, my question is again, is
there a way to include a .php file such that it is evaluated
outside of the context of the php script from which it is included, in
its own namespace, but so that the request for the file still comes from
the user's browser itself?

I haven't been able to find a way to do so; is there another solution to
my problem?


I'm still not clear to me exactly what you're asking for, but I just
wanted to double-check you are aware of the ability to define several
include_paths in order:

php_value include_path /path/to/1st/:/path/to/2nd/

I found this very useful on my server where I have about 30 sites. Many of
them use the same common code framework, in
/path/to/webroot/common/libraries/ but in same cases a more specific
version of a library is required, which is within the site itself, i.e.
/path/to/webroot/sitename/libraries/

hence

include_path
/path/to/webroot/sitename/libraries/:/path/to/webroot/common/libraries/

which means that if there is a local one, that will be used, otherwise it
will fall back to the generic version.
Martin Lucas-Smith www.geog.cam.ac.uk/~mvl22
www.lucas-smith.co.uk

Senior Computing Technician (Web Technician)
Department of Geography, University of Cambridge (01223 3)33390

& Webmaster, SPRI
Scott Polar Research Institute, University of Cambridge
Jul 16 '05 #7

P: n/a
In article
<Pi*************************************@green.csi .cam.ac.uk>,
Martin Lucas-Smith <mv***@cam.ac.uk> wrote:
I'm still not clear to me exactly what you're asking for, but I just
wanted to double-check you are aware of the ability to define several
include_paths in order:


[snip]

I think what you're missing is that I need to include the output of two
distinct classes in two distinct files at the same time, where the
classes have the same name, but different functions. The complication
which prevents the simple solution of using a full url
(inlude('http://www.example.com/foo.php')) is that the distinct files
make use of the user's cookies.

Thanks,

Nathan
Jul 16 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.