dpub33855 2017-07-18 18:34
浏览 55
已采纳

返回资源的无辜PHP函数?

Some PHP functions, like fopen(), have a return value of type "resource".

However, most of these functions require some actual outside resource, such as a file or database. Or they require additional PHP extension to be installed, such as curl_open().

I sometimes want to experiment with different value types on https://3v4l.org, where I cannot rely on external resources.

Another scenario where this might be relevant is unit tests, where we generally want as little side effects as possible.

So, what is the simplest way to get a value of type resource, without external side effects, 3rd party extensions, or external dependencies?

  • 写回答

3条回答 默认 最新

  • dongpo1216 2017-07-18 18:41
    关注

    You can use php://memory or php://temp as resource. The first one doesn't even need access to the system /tmp folder.

    Example:

    $resource = fopen('php://temp', 'w+');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?