Custom stream wrappers, what could they be useful for in web applications?
Posted
by michael
on Stack Overflow
See other posts from Stack Overflow
or by michael
Published on 2010-03-19T15:09:23Z
Indexed on
2010/03/19
15:11 UTC
Read the original article
Hit count: 160
I suppose the concept is language agnostic, but I don't know what it's called in other languages. In PHP they're Stream Wrappers. In short, a wrapper class that allows manipulation of a streamable resource (resource that can be read to/written to/seek into, such as a file, a db, an url).
For example, in a template engine (a view), upon including a template file such as:
include "view.wrapper://path/to/my/template/file.phtml";
my custom wrapper, declared elsewhere and associated with "view.wrapper", would first intercepts the file to replace such things as short tags (<?=)
with a more verbose counterpart (<?php echo)
. This allows developers to use short tags in views, even if the server isn't set to allow it. It can also be applied to the preprocessing of views pseudo syntax such as {@myVar}
(e.g. replacing it with $this->myVar
).
This is only one application of custom stream wrappers, but the feature seems powerful enough to make me think that there are others that could make life a lot simpler for developers.
- What have you built, or thought about building, custom stream wrappers for?
- where have you seen some interesting implementations?
I'm particularly interested in their applications in web development.
© Stack Overflow or respective owner