Posted by Stas on May 16, 2007
I wonder why we still have resource type in PHP?
Since 5.x, objects are perfectly capable on encapsulating any void * transparently (there’s at least 2 Java bridges doing that, for example) and of course using objects doesn’t force you to use OO syntax – i.e. you can do fread($foo) with $foo being either resource or object equally well. We can see ext/unicode/collator.c in PHP 6 as one example of dual interface also (I’m sure there are more, I just had to pick one). So objects as I see it can do anything resources can do. And much more – you could extend it (had we had file as object and not resource, streams probably would be much easier to implement), serialize it (provided correct methods of course), etc., etc.
Also, with some effort I think it would be possible to modify all resource-using code to use objects transparently – so all the scripts except for those that actually check the type to be “resource” (why one would do that anyway?) will keep working.
So, maybe it’s time to let the resource type go? Does anybody see any reason why resources are better than objects?