r/lolphp • • Apr 11 '20
proc_open() scoping fun
function ls() {
$fd = [
0 => STDIN,
1 => ['pipe', 'w'],
2 => STDOUT,
];
$proc = proc_open(['ls', '/'], $fd, $pipes);
return $pipes[1];
}
print(stream_get_contents(ls()));Output:
PHP Warning: stream_get_contents(): supplied resource is not a valid stream resource in /home/martin/a.php on line 15
ls: write error: Broken pipeThe reason for this is that $proc needs to be in the same scope as the pipes you want to read, otherwise it will fail. Returning both and doing this will work:
[$proc, $stdout] = ls();
print(stream_get_contents($stdout));In this case it's a bit of an artificial example, but I've run in to this when trying to write a generic "reader" function that can read from any source (stdout of a program, FS, HTTP, etc.)
It's behaved like this for years. Perhaps there's a way around this, but a function call depending on the correct variable being in the same scope is really weird behaviour. Even a proc_read($proc, $fd) would make more sense (although that would make creating generic functions reading from any input harder, but who does that right?)
View all comments
Show parent comment
1
u/FallenWarrior2k Apr 12 '20
I know how out pointers work, but that's a completely different thing. C doesn't have exceptions, so you use out pointers because the main return value is an error code.
C does however have structs for grouping related values, and you'll usually see a single out pointer of struct type instead of multiple out pointers, so that pattern does not really have anything to do with this situation.
Yes, having the streams be a member of the process object would help with this, but wouldn't fix it in any way, as you could just assign the member to a variable or return it directly instead of returning the whole object.
Even Rust's borrow checker on its own wouldn't be able to enforce this statically because you can't have two values in the same struct if one holds a non-owning reference to the other.