rfc:streamwrapper-factory

Request for Comments: Factory for Stream Wrappers

Introduction

This RFC proposes to add functionality for creating stream instances through a factory callback instead of having php internals create a new instance using “$instance = new $class” internally.

The benefits of this approach are:

1. Allows to inject state into stream wrappers from other parts of the application without having to access global state or encode information into the stream uri. 2. Increases testability of code using stream wrappers.

Registering a callback for a stream comes with some requirements that are implicit:

1. You have to return an instance on every call of the factory (otherwise the code fails with an error (magnitude to be determined)) 2. You should return a new instance on every call, otherwise you'd have to do some wiggling to get the internal state of the instance managing several streams right.

Implementation

Registering a callback instead of a class-name would be done by adding a third parameter to stream_wrapper_register:

class gopher_stream
{
    // implement some stream methods
}
 
function gopher_factory()
{
    return new gopher_stream();
}
 
stream_wrapper_register("gopher", "gopher_factory", STREAM_IS_URL | STREAM_USE_FACTORY);

Patch

Changelog

  • 2011-09-11: Initial version
rfc/streamwrapper-factory.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1