You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@thrift.apache.org by Dvir Volk <dv...@gmail.com> on 2009/07/20 14:34:05 UTC

wrong include paths in PHP

Hi,
When I'm generating php from thrift files, I always get this thing:
if I generate a service called Foo, the output is 2 files, one is
FooService.php and the other Foo_types.php, FooService.php will contain the
following include:

include_once $GLOBALS['THRIFT_ROOT'].'/packages/Foo/Foo_types.php';

when actually they are both located in the the same directory, and it should
just be:
include_once 'Foo_types.php';

How do I stop this behavior? It's driving me crazy editing generated files
all the time...

Re: wrong include paths in PHP

Posted by Dvir Volk <dv...@gmail.com>.
I will, in the meantime, writing a compile-n-copy script will be faster, and
most definitely faster than rewriting the php file upon generation.

On Mon, Jul 20, 2009 at 10:50 PM, Mark Slee <ms...@facebook.com> wrote:

> Yes, this is true, the default generator doesn't automatically place the
> files into this directory structure. We just sort of assumed most people
> using PHP stuff were writing stuff for a webserver, and were going to need
> to copy their generated code over into some other file hierarchy anyways.
>
> Definitely feel free to make a patch or a compiler flag that lets you more
> easily manipulate the generated directories.
>
> -----Original Message-----
> From: Dvir Volk [mailto:dvirsky@gmail.com]
> Sent: Monday, July 20, 2009 12:39 PM
> To: thrift-user@incubator.apache.org
> Subject: Re: wrong include paths in PHP
>
> I usually use dirname(__FILE__) or $_SERVER['DOCUMENT_ROOT'] to
> "absolutize"
> the paths before including stuff.
>
> But anyway, even if I want to follow thrift's include logic, it still means
> i have to copy my files to the right path manually,
> since thrift --gen php -o <somedir> generates the file under "gen-php" in
> this dir, and the generated code expects the types file to sit under
> $GLOBALS['THRIFT_ROOT'].'/packages/<MyService>/'.
> Doesn't it?
>
>
>
>
> On Mon, Jul 20, 2009 at 10:21 PM, Mark Slee <ms...@facebook.com> wrote:
>
> > The issue here is because of the way that PHP resolves include paths.
> It's
> > really bizarre and unexpected, but when you say "include <relative path>"
> in
> > PHP the path is NOT relative to the file that contains the statement, but
> > rather it is relative to the directory from which the PHP interpreter was
> > originally invoked.
> >
> > This is fine if you dump all your code into one big folder and run your
> > interpreter from there, but if you want to organize your code into
> folders,
> > the cleanest viable solution is to just switch to using absolute paths to
> > reference your files. This is why we define a THRIFT_ROOT and declare all
> > generated includes relative to that.
> >
> > There are also some benefits to using absolute paths if you are running
> PHP
> > on Apache with APC byte-code caching enabled. The absolute paths allow
> you
> > to avoid doing filesystem operations when you're checking for a script's
> > presence in the cache, you can just treat the absolute path as a simple
> > string key.
> >
> > -----Original Message-----
> > From: Dvir Volk [mailto:dvirsky@gmail.com]
> > Sent: Monday, July 20, 2009 5:52 AM
> > To: thrift-user@incubator.apache.org
> > Subject: Re: wrong include paths in PHP
> >
> > I could, but I figured since in python it's not mandatory, there might be
> a
> > trick to achieve that in PHP as well...
> >
> > On Mon, Jul 20, 2009 at 3:37 PM, Patrick Schlangen <p.schlangen@mac.com
> > >wrote:
> >
> > > Hi,
> > >
> > > why don't you just use the directory structure suggested by Thrift?
> > > It's very logical and tidy.
> > >
> > > Patrick
> > >
> > > Am Montag 20 Juli 2009 um 02:34PM schrieb "Dvir Volk" <
> dvirsky@gmail.com
> > >:
> > > >Hi,
> > > >When I'm generating php from thrift files, I always get this thing:
> > > >if I generate a service called Foo, the output is 2 files, one is
> > > >FooService.php and the other Foo_types.php, FooService.php will
> contain
> > > the
> > > >following include:
> > > >
> > > >include_once $GLOBALS['THRIFT_ROOT'].'/packages/Foo/Foo_types.php';
> > > >
> > > >when actually they are both located in the the same directory, and it
> > > should
> > > >just be:
> > > >include_once 'Foo_types.php';
> > > >
> > > >How do I stop this behavior? It's driving me crazy editing generated
> > files
> > > >all the time...
> > > >
> > > >
> > >
> >
>

RE: wrong include paths in PHP

Posted by Mark Slee <ms...@facebook.com>.
Yes, this is true, the default generator doesn't automatically place the files into this directory structure. We just sort of assumed most people using PHP stuff were writing stuff for a webserver, and were going to need to copy their generated code over into some other file hierarchy anyways.

Definitely feel free to make a patch or a compiler flag that lets you more easily manipulate the generated directories.

-----Original Message-----
From: Dvir Volk [mailto:dvirsky@gmail.com] 
Sent: Monday, July 20, 2009 12:39 PM
To: thrift-user@incubator.apache.org
Subject: Re: wrong include paths in PHP

I usually use dirname(__FILE__) or $_SERVER['DOCUMENT_ROOT'] to "absolutize"
the paths before including stuff.

But anyway, even if I want to follow thrift's include logic, it still means
i have to copy my files to the right path manually,
since thrift --gen php -o <somedir> generates the file under "gen-php" in
this dir, and the generated code expects the types file to sit under
$GLOBALS['THRIFT_ROOT'].'/packages/<MyService>/'.
Doesn't it?




On Mon, Jul 20, 2009 at 10:21 PM, Mark Slee <ms...@facebook.com> wrote:

> The issue here is because of the way that PHP resolves include paths. It's
> really bizarre and unexpected, but when you say "include <relative path>" in
> PHP the path is NOT relative to the file that contains the statement, but
> rather it is relative to the directory from which the PHP interpreter was
> originally invoked.
>
> This is fine if you dump all your code into one big folder and run your
> interpreter from there, but if you want to organize your code into folders,
> the cleanest viable solution is to just switch to using absolute paths to
> reference your files. This is why we define a THRIFT_ROOT and declare all
> generated includes relative to that.
>
> There are also some benefits to using absolute paths if you are running PHP
> on Apache with APC byte-code caching enabled. The absolute paths allow you
> to avoid doing filesystem operations when you're checking for a script's
> presence in the cache, you can just treat the absolute path as a simple
> string key.
>
> -----Original Message-----
> From: Dvir Volk [mailto:dvirsky@gmail.com]
> Sent: Monday, July 20, 2009 5:52 AM
> To: thrift-user@incubator.apache.org
> Subject: Re: wrong include paths in PHP
>
> I could, but I figured since in python it's not mandatory, there might be a
> trick to achieve that in PHP as well...
>
> On Mon, Jul 20, 2009 at 3:37 PM, Patrick Schlangen <p.schlangen@mac.com
> >wrote:
>
> > Hi,
> >
> > why don't you just use the directory structure suggested by Thrift?
> > It's very logical and tidy.
> >
> > Patrick
> >
> > Am Montag 20 Juli 2009 um 02:34PM schrieb "Dvir Volk" <dvirsky@gmail.com
> >:
> > >Hi,
> > >When I'm generating php from thrift files, I always get this thing:
> > >if I generate a service called Foo, the output is 2 files, one is
> > >FooService.php and the other Foo_types.php, FooService.php will contain
> > the
> > >following include:
> > >
> > >include_once $GLOBALS['THRIFT_ROOT'].'/packages/Foo/Foo_types.php';
> > >
> > >when actually they are both located in the the same directory, and it
> > should
> > >just be:
> > >include_once 'Foo_types.php';
> > >
> > >How do I stop this behavior? It's driving me crazy editing generated
> files
> > >all the time...
> > >
> > >
> >
>

Re: wrong include paths in PHP

Posted by Dvir Volk <dv...@gmail.com>.
I usually use dirname(__FILE__) or $_SERVER['DOCUMENT_ROOT'] to "absolutize"
the paths before including stuff.

But anyway, even if I want to follow thrift's include logic, it still means
i have to copy my files to the right path manually,
since thrift --gen php -o <somedir> generates the file under "gen-php" in
this dir, and the generated code expects the types file to sit under
$GLOBALS['THRIFT_ROOT'].'/packages/<MyService>/'.
Doesn't it?




On Mon, Jul 20, 2009 at 10:21 PM, Mark Slee <ms...@facebook.com> wrote:

> The issue here is because of the way that PHP resolves include paths. It's
> really bizarre and unexpected, but when you say "include <relative path>" in
> PHP the path is NOT relative to the file that contains the statement, but
> rather it is relative to the directory from which the PHP interpreter was
> originally invoked.
>
> This is fine if you dump all your code into one big folder and run your
> interpreter from there, but if you want to organize your code into folders,
> the cleanest viable solution is to just switch to using absolute paths to
> reference your files. This is why we define a THRIFT_ROOT and declare all
> generated includes relative to that.
>
> There are also some benefits to using absolute paths if you are running PHP
> on Apache with APC byte-code caching enabled. The absolute paths allow you
> to avoid doing filesystem operations when you're checking for a script's
> presence in the cache, you can just treat the absolute path as a simple
> string key.
>
> -----Original Message-----
> From: Dvir Volk [mailto:dvirsky@gmail.com]
> Sent: Monday, July 20, 2009 5:52 AM
> To: thrift-user@incubator.apache.org
> Subject: Re: wrong include paths in PHP
>
> I could, but I figured since in python it's not mandatory, there might be a
> trick to achieve that in PHP as well...
>
> On Mon, Jul 20, 2009 at 3:37 PM, Patrick Schlangen <p.schlangen@mac.com
> >wrote:
>
> > Hi,
> >
> > why don't you just use the directory structure suggested by Thrift?
> > It's very logical and tidy.
> >
> > Patrick
> >
> > Am Montag 20 Juli 2009 um 02:34PM schrieb "Dvir Volk" <dvirsky@gmail.com
> >:
> > >Hi,
> > >When I'm generating php from thrift files, I always get this thing:
> > >if I generate a service called Foo, the output is 2 files, one is
> > >FooService.php and the other Foo_types.php, FooService.php will contain
> > the
> > >following include:
> > >
> > >include_once $GLOBALS['THRIFT_ROOT'].'/packages/Foo/Foo_types.php';
> > >
> > >when actually they are both located in the the same directory, and it
> > should
> > >just be:
> > >include_once 'Foo_types.php';
> > >
> > >How do I stop this behavior? It's driving me crazy editing generated
> files
> > >all the time...
> > >
> > >
> >
>

RE: wrong include paths in PHP

Posted by Mark Slee <ms...@facebook.com>.
The issue here is because of the way that PHP resolves include paths. It's really bizarre and unexpected, but when you say "include <relative path>" in PHP the path is NOT relative to the file that contains the statement, but rather it is relative to the directory from which the PHP interpreter was originally invoked.

This is fine if you dump all your code into one big folder and run your interpreter from there, but if you want to organize your code into folders, the cleanest viable solution is to just switch to using absolute paths to reference your files. This is why we define a THRIFT_ROOT and declare all generated includes relative to that.

There are also some benefits to using absolute paths if you are running PHP on Apache with APC byte-code caching enabled. The absolute paths allow you to avoid doing filesystem operations when you're checking for a script's presence in the cache, you can just treat the absolute path as a simple string key.

-----Original Message-----
From: Dvir Volk [mailto:dvirsky@gmail.com] 
Sent: Monday, July 20, 2009 5:52 AM
To: thrift-user@incubator.apache.org
Subject: Re: wrong include paths in PHP

I could, but I figured since in python it's not mandatory, there might be a
trick to achieve that in PHP as well...

On Mon, Jul 20, 2009 at 3:37 PM, Patrick Schlangen <p....@mac.com>wrote:

> Hi,
>
> why don't you just use the directory structure suggested by Thrift?
> It's very logical and tidy.
>
> Patrick
>
> Am Montag 20 Juli 2009 um 02:34PM schrieb "Dvir Volk" <dv...@gmail.com>:
> >Hi,
> >When I'm generating php from thrift files, I always get this thing:
> >if I generate a service called Foo, the output is 2 files, one is
> >FooService.php and the other Foo_types.php, FooService.php will contain
> the
> >following include:
> >
> >include_once $GLOBALS['THRIFT_ROOT'].'/packages/Foo/Foo_types.php';
> >
> >when actually they are both located in the the same directory, and it
> should
> >just be:
> >include_once 'Foo_types.php';
> >
> >How do I stop this behavior? It's driving me crazy editing generated files
> >all the time...
> >
> >
>

Re: wrong include paths in PHP

Posted by Dvir Volk <dv...@gmail.com>.
I could, but I figured since in python it's not mandatory, there might be a
trick to achieve that in PHP as well...

On Mon, Jul 20, 2009 at 3:37 PM, Patrick Schlangen <p....@mac.com>wrote:

> Hi,
>
> why don't you just use the directory structure suggested by Thrift?
> It's very logical and tidy.
>
> Patrick
>
> Am Montag 20 Juli 2009 um 02:34PM schrieb "Dvir Volk" <dv...@gmail.com>:
> >Hi,
> >When I'm generating php from thrift files, I always get this thing:
> >if I generate a service called Foo, the output is 2 files, one is
> >FooService.php and the other Foo_types.php, FooService.php will contain
> the
> >following include:
> >
> >include_once $GLOBALS['THRIFT_ROOT'].'/packages/Foo/Foo_types.php';
> >
> >when actually they are both located in the the same directory, and it
> should
> >just be:
> >include_once 'Foo_types.php';
> >
> >How do I stop this behavior? It's driving me crazy editing generated files
> >all the time...
> >
> >
>

Re: wrong include paths in PHP

Posted by Patrick Schlangen <p....@mac.com>.
Hi,

why don't you just use the directory structure suggested by Thrift?
It's very logical and tidy.

Patrick
 
Am Montag 20 Juli 2009 um 02:34PM schrieb "Dvir Volk" <dv...@gmail.com>:
>Hi,
>When I'm generating php from thrift files, I always get this thing:
>if I generate a service called Foo, the output is 2 files, one is
>FooService.php and the other Foo_types.php, FooService.php will contain the
>following include:
>
>include_once $GLOBALS['THRIFT_ROOT'].'/packages/Foo/Foo_types.php';
>
>when actually they are both located in the the same directory, and it should
>just be:
>include_once 'Foo_types.php';
>
>How do I stop this behavior? It's driving me crazy editing generated files
>all the time...
>
>