include() in separate namespace?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nathan Lamont

    include() in separate namespace?

    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
  • matty

    #2
    Re: include() in separate namespace?

    Nathan Lamont wrote:
    [color=blue]
    > 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.[/color]

    Just use

    include_once('c lassfile.php');

    or
    require_once('c lassfile.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('includ efiles/'.$myclassname. '.php');
    [color=blue]
    > 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?[/color]

    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

    Comment

    • Nathan Lamont

      #3
      Re: include() in separate namespace?

      In article <a_53b.341$4L1. 65610@wards.for ce9.net>,
      matty <matt+nntp@askm enoquestions.co .uk> wrote:

      [snip]
      [color=blue]
      > 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[/color]

      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

      Comment

      • matty

        #4
        Re: include() in separate namespace?

        Nathan Lamont wrote:
        [color=blue]
        > In article <a_53b.341$4L1. 65610@wards.for ce9.net>,
        > matty <matt+nntp@askm enoquestions.co .uk> wrote:
        >
        > [snip]
        >[color=green]
        >> 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[/color]
        >
        > 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?
        >[/color]

        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

        Comment

        • André Næss

          #5
          Re: include() in separate namespace?

          Nathan Lamont:
          [color=blue]
          > 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?[/color]

          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

          Comment

          • Nathan Lamont

            #6
            Re: include() in separate namespace?

            In article <Dp63b.359$4L1. 69242@wards.for ce9.net>,
            matty <matt+nntp@askm enoquestions.co .uk> wrote:
            [color=blue]
            > 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![/color]

            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

            Comment

            • Martin Lucas-Smith

              #7
              Re: include() in separate namespace?



              [color=blue][color=green]
              > > 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?[/color][/color]

              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


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

              & Webmaster, SPRI
              Scott Polar Research Institute, University of Cambridge


              Comment

              • Nathan Lamont

                #8
                Re: include() in separate namespace?

                In article
                <Pine.SOL.4.44. 0308271942230.3 895-100000@green.cs i.cam.ac.uk>,
                Martin Lucas-Smith <mvl22@cam.ac.u k> wrote:
                [color=blue]
                > 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:[/color]

                [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

                Comment

                Working...