Kill resources

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?

About these ads

5 thoughts on “Kill resources

  1. Pingback: developercast.com » PHP 10.0 Blog: Kill resources

  2. Kill them? No. But I agree that on a go-forward basis, objects should be encouraged. They’re just a lot more versatile than resources are. As for speed… I’m inclined to doubt Johannes’ claims. The object store is a vector, while the resource list is a hash table. Resources might (and I stress might) come out ahead on some benches, but objects are going to come out ahead on others. The balance is probably closer than you think…

    Of course, I could be wrong too….

  3. Another prominent candidate is ext/mysqli. I, though, think that there’s a bit more code involved for mainitaining objects compared to resources, but–honestly–I didn’t spend lots of time with the resource API :)

  4. Why do you thinki they are always faster? As far as I can see, same amount of code is in play in both cases.

    As for persisting between requests, void* can be, resource zval can’t really be. Same with objects – we can keep the void*, and create zval on need. It would cost roughly the same in both cases.

Comments are closed.