PHP 10.0 Blog

What if…

Kill resources

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?

About these ads

5 Responses to “Kill resources”

  1. Johannes said

    Resources are faster and can be persistent over requests.

  2. Stas said

    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.

  3. Mike said

    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. 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….

  5. [...] a new post to the PHP 10.0 blog today, Stas asks the question “Why do we still have resource type in [...]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

%d bloggers like this: