Quantcast
Channel: How To – Paul K Leasure
Viewing all articles
Browse latest Browse all 39

How to Understand PHP Stream Wrapper Context and Options

$
0
0

Some have a difficult time understanding what is meant in PHP by stream wrappers, context options and parameters. Judging by a few conversations with other developers, its obvious there is a bit of confusion about what a stream wrapper is and why it would have a context. While the PHP documentation does adequately describe what this is, some just don’t “get it” the way that is described.

So if the descriptions of PHP Streams and Wrappers and contexts just make no sense when you read them, the following illustration is meant for you.

OK, picture yourself as a person in an assembly line. Whatever is coming down the line you need to be able to “handle it” and pass it along. Thing is, there are several possibilities of what might be coming down the line. It might be a hot casserole in metal pan. It might be a block of ice. It might be candy that needs to be wrapped. It might be glowing hot metal from a furnace. It might be liquid being poured from bucket to bucket. Can you see it in your minds eye?

OK, so if its liquid being poured from bucket to bucket, then you need a bucket, right? If it is a glowing hot piece of metal you better have your tongs handy! If it is a casserole fresh out of the oven then you need to have your stove mits on. If you being handed of a block of ice, then your large ice tongs will allow you to grasp it and pass it along.

The point is, to properly handle whatever is coming down the line (streamed data), you will need the appropriate tool(s) to handle the different kinds of items being passed (data types). The tools for handling each different kind of data type are the “parameters”. The parameters are like the tongs for the hot metal, the oven mits for the casserole or the foil in hand.

But how do you know which way of handling what is coming down the pipe? Simple, that is based on what the situation is. You know which type of item it is (which data type).

con·text (see link definition #2) synonyms: circumstances, conditions, factors

The “context” is determined by what exactly is being streamed. So for a different “contexts” (kinds of items) being “streamed” (passed along) the “parameters” (required tools for handling) the “data type” (kind of item) will change.

So for different data types being streamed the parameters will change. The term context simply makes reference to the fact that for different data types the situation is unique with its own required parameters. So now it hopefully makes sense to you that the PHP stream wrapper would require a context in order to know which parameters are needed to handle the data type.

The term “stream” makes reference to something being passed in or out of the application (on the assembly line).

The stream wrapper is code and relates to the person on the assembly line who is responsible for passing the stream along. It “decides” how to handle what is being passed.

The context is determined by WHAT is being passed along.
Based on the context, the option and parameters will change to properly handle what data type is being handled.

With that in mind, the concept is really quite simple. HTTP has options and parameters that are different from cURL options and parameters. PHP stream wrapper FTP options that differ from say SSL context options and so on.

Hopefully, with this understanding to help grasp what’s happening with PHP stream wrappers and contexts, reading the PHP stream wrapper documentation will make a lot more sense.


Viewing all articles
Browse latest Browse all 39

Trending Articles