Chapter 11. Streams
Streams in PHP represent common interfaces to data resources that can be written to or read from in a linear, continuous manner. Internally, each stream is represented by a collection of objects referred to as buckets. Each bucket represents a chunk of data from the underlying stream, which is treated like a digital re-creation of an old-fashioned bucket brigade, like the one illustrated in Figure 11-1.
Bucket brigades were often used to transport water from a river, stream, lake, or well to the source of a fire. When it was impossible to use hoses to move the water, people would line up and pass buckets from one to another in order to fight the fire. One person would fill a bucket at the water source and then pass the bucket to the next person in line. The people in line didn’t move, but the bucket of water was transported from person to person in turn until the final person could throw the water on the fire. This process would continue until either the fire was extinguished or the source ran out of water.
Though you’re not using PHP to fight a fire, the internal structure of a stream is somewhat similar to a bucket brigade because of the way data is passed one chunk (bucket) at a time through whatever component of code is processing it.
Generators are also analogous to this pattern. ...
Get PHP Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.