For my sins, I had a hand in what filters on PHP streams (file I/O resources) looked like in PHP 4 days, and I’ve regretted how they turned out ever since.
This evening, I’m about two shots of tequila from proposing to rip them out rather than fight my way through converting them from resource to objects (as part of https://github.com/php/php-tasks/issues/6 ).


Obviously killing them entirely would be a slight BC break (and would have to wait for 9.0, so I’ve probably already missed that train), but what I want to know is: How much do y’all find yourselves working with them?
Pulling out a bit further: How might you re-invent streams as a whole in PHP 9.0 if you could have your way? Are there popular abstractions in frameworks or libraries out there you wish were standard? Got a gripe about I/O in general you just want to vent?
The lack of documentation always put me off userspace stream filters. Because of that I’ve only turned to them when I’ve had to hack together something horrible. I’ve felt clever in the bad way whenever I’ve managed to accomplish something with them. My association with them is entirely negative.
Moreover I don’t feel qualified talking about them because my understanding, and therefore experience, and therefore understanding of them is so limited.
I don’t care what you do with them as long as whatever the result is is documented. There should be no associated page in the manual that still says:
Warning This function is currently not documented; only its argument list is available.
I will take a shitty but well-documented api over anything I have to guess at whether I understand.
Addendum:
The most useful info I ever found on them was Bob’s answer on StackOverflow, and even that threw me off for a long time because a bug in PHP had broken his basic example.
I’ve programmed in PHP for over 22 years now (March 1998, baby!). I have never once even seen the streams filters in live code, nor wrote them…
I searched GitHub and a single project uses stream_filter_register, but all I got were ~100,000 copies of PHP’s own .phpt test files that use them.
I’m now searching the entire Bettergist Collective (all 250,000+ reachable PHP projects on packagist) for stream_filter_register. It’s quite rare…
adgoal/goaop-framework/src/Instrument/ClassLoading/SourceTransformingLoader.php
adhocore/cli/tests/CliTestCase.php
ajgl/csv-rfc/src/CsvRfcWriteStreamFilter.php
appserver-io/properties/src/AppserverIo/Properties/PropertiesUtil.php
bakame/csv/src/CharsetConverter.php
bearer/php-agent/Stream/Transformer.php
bearlord/aop/src/SourceTransformingLoader.php
clue/stream-filter/src/functions.php
duncan3dc/bom-string/tests/StreamFilterTest.php
eloquent/lockbox/src/Lockbox.php
enorris/mbstring-stream/src/MultibyteStringStream.php
mwlib/tests/MW/Process/PcntlTest.php
php-http/message/src/filters.php
phpmd/src/test/php/PHPMD/TextUI/CommandTest.php
I didn’t search anything after e. (That’s over 27% of all Packagist packages right there and I didn’t feel like busting out the whole 500 GB working copy hard drive).
11 out of 77,782 packages is a very very small amount (0.01%).
22-33 packages, across all of PHP’s packagist.org packages apparently use the stuff. I’d say if there’s a good enough reason, we could redo them in 8.1 :p


Yeah. That’s the kind of data I’m seeing as well and it’s what make me think we can just wholesale deprecate the old APIs. The question is how much (or little) do we need a replacement?
/u/nikita2206 makes a point about filtering through include/require, but even there’s it’s done by way of the php://filter stream wrapper, and they have a much better user-space implementation than filters do. It might be fine to expect libraries/frameworks to define wrappers which do filtration as a byproduct rather than have filtration built in.
Stream filters as in these? Never once used them, and I’ve been in some pretty obscure areas and committed plenty of my own language sins. The filter_var stuff I’ve used a couple times but wouldn’t miss that much.
My thoughts on streams, file io, and such definitely require me being not on mobile to type out.
Hey Sara, I think a good use case for those was to hook in the require/include to rewrite code on the fly. Other than that I’m not sure they do anything that couldn’t be done easier without them? What do you think of, instead of stream filters, introducing a native way to manipulate the code or maybe byte code (it has to be viewable by the developer though) during runtime and potentially during compile time (if we ever start compiling PHP to byte code for deployments)? Something close to Java’s class loaders but not as convoluted. For instance in the Java ecosystem there’s a Lombok library which most of the time is used with some of its built-in code generation strategies (like generating getters) but its real feature IMO is being able to create your own code transformations which can help with condensing typical web app codebase where you see lots of repetitive annotations for example: http://notatube.blogspot.com/2010/12/project-lombok-creating-custom.html
I think a good use case for those was to hook in the require/include to rewrite code on the fly.
Right. Good point. However, I think any of these could be done equally well with custom wrappers using a proxy pattern (which is a de-facto filter at that point).
How might you re-invent streams as a whole in PHP 9.0 if you could have your way?
Not with objects….my understanding of streams is about as good as my understanding of other parts of the engine, but I think I’d expose the structs that are held internally as being programmable from userland.
i.e. expose what is possible in the C code in userland, and then go from there.
I would say drastic measures can be used. I have never ever even remotely heard anyone using those. And as /u/2012-09-04 pointed out with some research, their usage is so rare that you can even reach out to specific authors and talk about it and see how it impacts their projects if at all and what can be done to improve/change/rip them out 🙂
I am probably confusing filters and wrappers, which is part of the problem. It’s been a long long time since I had to use either, but I do recall doing so once upon a time. I know Drupal had some filter/wrapper classes in them at some point that everyone hated, but I don’t recall what they did. It does make use of custom streams to handle “read a theme file from the directory where the theme is, which could be one of 5 places.”
In the back of my head, I have this vision of using the pipe operator to string together user-space callables that operate on IO streams, which can then be saved to another function. Especially if along the way you can also switch from bytestream to chunked objects, generator style, as that allows you to do something that looks kinda like functional-reactive if you squint.
Something kinda vaguely sorta like:
Would that actually be doable or make sense, I have no idea, but I like it in my head…
This is a bit off-topic but I can’t help but wonder how many PHP design decisions are made under the influence of alcohol?
It would explain so much, wouldn’t it?
Members
Online

source