Re: Additions to PHP_Compat
| From: | Davey | Date: | Wed, 26 May 2004 18:59:25 +0000 |
| Subject: | Re: Additions to PHP_Compat | ||
| References: | 1 2 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-29724@lists.php.net to get a copy of this message | ||
Stefan Neufeind wrote:
On 26 May 2004 at 18:47, Lorenzo Alberton wrote:Stefan the value of a define() is ONLY evaluated at the time it's define()'ed. otherwise: define('FOO', (3 * 7) / 8) would do that math every time you call the FOO constant - this isn't the case, FOO is simply assigned the result. - DaveyOn Wed, 26 May 2004 18:09:30 +0200, Michael Wallner wrote:Just a quick test I tried (php 4.3.4-cli on Linux): <? define('ASTDIN', fopen('php://stdin', 'r')); define('ASTDOUT', fopen('php://stdout', 'w')); var_dump($a = ASTDIN); var_dump($b = ASTDIN); var_dump($a === $b); var_dump($c = ASTDOUT); ?> gives: resource(1) of type (stream) resource(1) of type (stream) bool(true) resource(2) of type (stream) So I suppose that PHP doesn't really "open" stdin, but returns the same stdin-resource again? Would be nice if somebody with a debugger could check if fopen is called twice ... in this case we could save some time using Mike's workaround he mentioned above. Anyway, I think such functionality for "really old php" is a good idea. StefanHi Stefan Neufeind, you wrote:I think Stefan's worries have some ground... each time I *use* the constant STDIN, a fopen call will be issued (is it correct?). I never tried something like that, but the definition above looks more like a C macro than a define... What about: =========== if (!defined('STDIN')) { $_stdin = fopen('php://stdin', 'r'); define('STDIN', &$_stdin); } =========== of course it is absolutely untested, but it should give an idea of what Stefan was trying to say and a possible solution (?). When I use the STDIN constant, I want THE pointer back, not a NEW pointer each time...I hope I got you right: Once STDIN is defined it won't step into the if condition any more, so php://stdin etc. is only opened once.Could these be added to PHP_Compat? if (!defined('STDIN')) { define('STDIN', fopen('php://stdin', 'r')); } --- if (!defined('STDOUT')) { define('STDOUT', fopen('php://stdout', 'w')); } --- if (!defined('STDERR')) { define('STDERR', fopen('php://stderr', 'w')); }If you use it in a program that e.g. does multiple writes to STDOUT, am I wrong that this would open the file again and again for each of the output-calls, resulting in many open files and never (until script end) closing any of these? Or do these calls always return the same handler to the stdout etc. (not tested)?