Davey Shafik wrote on 23/11/2015 16:17:
How is this a breaking change? The only way you can get a resource, or
resource-object is from a function that currently returns a resource
(e.g. imagecreate()), and you can't do anything with a resource except
pass into things that expect resources. If instead they return an
object, and every function that currently expects a resource now
expects that kind of resource-object, it could just work…
Within those functions, it's fine, but there are a handful of "meta"
functions which can tell the difference between a resource and an
object, and these may be used in existing code for error handling or
switching behaviour - e.g.
switch ( gettype($file) )
{
case 'string':
// code to open a new handle
break;
case 'resource':
// code to use the open handle
break;
case 'object'
if ( $file instanceOf MyFileWrapperInterface ) // ...
break;
default:
// error
}
Would it be possible to create a (possibly empty, but maybe useful)
interface that all of the new resource-objects implement, meaning
is_resource() and gettype() can check for that going forward and
return a BC result (and if necessary, leave the current implementation
in there as a code path for custom extensions maybe). You could also
type hint off that interface, either transparently replacing the
proposed type hint, or instead of.
This certainly helps, but you've still got to break something, because
currently is_resource() and is_object() are mutually exclusive, and
gettype() can only return one string, so you've got to break somebody's
code somewhere. (Cue xkcd "every change breaks somebody's flow" comic;
but I think the example above demonstrates that this is a non-trivial
break.)
But I would definitely prefer to see a CurlResource or ImageResource
object instead of a resource type hint.
Definitely agree here.
The only thing I see as contentious, is actually, is_object() — should
that return false, maintaining BC, or true?
I think it would be weird not to have it return true - otherwise in 10
years time, there'd be a weird note in the manual saying
"is_object($var): returns true if $var is an object, unless it is one of
the following internal types..." gettype() returning 'resource' for an
object would be similarly surprising.
More contentious is how is_resource() should behave; assuming *all*
resources are converted, it could just be deprecated anyway, I guess...
Regards,
--
Rowan Collins
[IMSoP]
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php