On 04/12/2014 23:46, Chris Wright wrote:
Forgot to cc list, see below

---------- Forwarded message ----------
From: "Chris Wright" <c...@daverandom.com>
Date: 4 Dec 2014 14:09
Subject: Re: [PHP-DEV] Re: Only variables can be passed by reference
To: "Andrea Faulds" <a...@ajf.me>
Cc:

On 4 December 2014 at 12:26, Andrea Faulds <a...@ajf.me> wrote:

On 4 Dec 2014, at 10:38, Yasuo Ohgaki <yohg...@ohgaki.net> wrote:


Yet another sample code. This cannot be executed on 3v4l.org for
security
reasons.

<?php
$sock = fsockopen('www.php.net', '80');
var_dump($sock);

$socks = array($sock);
var_dump(stream_select($socks, $socks, $socks, 1));

//var_dump(stream_select(array($sock), array($sock), array($sock), 1));
//Fatal error: Only variables can be passed by reference in
/home/yohgaki/tmp/ttt.php on line 8
?>
Using stream_select without real variables like that is weird, what’s
the point? You’ve discarded the read/write/error information, all the
return value tells you is that *something* changed.
While I'm undecided on this change (leaning towards "no"), there is a
more useful example with stream_select():
$socks = array($sock);
var_dump(stream_select($socks, $socks, NULL, 1));

It is rare that anything actually uses OOB data in the real world, more
often than not stream_select() is preceded by a line like $e = NULL;
I could maybe get behind this for specifically permitting literal NULL to
be passed, as is often the convention for out params in C, where passing a
NULL pointer is permitted and handled as "I don't want this data".
Another example of where this could be useful is stream_socket_client(),
where you want to specify timeout/flags/context but don't care about
errno/errstr, so you could pass literal NULL to these instead of having to
declare a couple of trash variables.
That said, the use cases above would probably be better served by named
parameters.


Perhaps if a parameter is optional and defaulted to null, a literal null could be allowed even if it would normally be by-ref? This would match the behaviour of type-hinting, e.g.

function foo(Bar $bar=null) { ... }
foo(null);

function foo(&$bar=null) { ... }
foo(null);

The point being that you could then skip over by-ref parameters in some of the examples above, e.g. allow this:

$sock = fsockopen('www.php.net', '80');
$socks = array($sock);
stream_select($socks, null, null, 1);

Named parameters would make this look prettier, but you'd need to make the parameters optional in the declaration anyway.

--
Rowan Collins
[IMSoP]


--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to