tekx – streams, sockets and filters

Image of auroraeosrose from Twitter
Image of auroraeosrose

@auroraeosrose‘s talk. Going over definitions so that everyone is on the same page.

Everything uses streams

  • include/require
  • stream functions
  • file system functions
  • many other extensions

What is a stream in php? They allow you access input and output very generically. You can read and write linearly and may or may not be seekable. Comes in chunks of data. Think of a 15GB file, would you want to read that into memory? Of course not, PHP will laugh at you for being stupid.

Thank Sara Goleman and Wez Furlong for the awesomeness of streams.

Edit: Originally didn’t get Wez’s name in the presentation, he thankfully provided me with his identity.

Talking about file_get_contents. What out for flock, transport and wrapper limitations, non-existent pointers (infinite loops can and will happen) and error handling. flock is broken on mod_php and network shares so don’t use it if you have those. The only way you have to handle errors is e_errors so you’ll have to make an error handler or shutup. Don’t use shutup, you’re a nub if you do. It’s better to take the time to make a custom handler.

Filters

Filters allow you to perform operations on stream data. They can be appended or prepended as well as can be attached to read or write. When a filter is added for read and write, two instances of the filter are created. It is advised to attach to read or write but not both even though both is possible. Watch your modes because filters are smart and will attach to the mode you assign to your stream.

Filter gotchas

  • data has an input and output state
  • when reading in chunks, you may need to cache in between reads to make filters useful
  • use the right tool for the job

Sockets

  • Network stream, network transport, socket transport
  • slightly different behavior from a file stream
  • bi-directional data (it goes both ways baby)

Best definition @auroraeosrose has heard is from UNIX that describes sockets as wormholes.

Sockets extension lets you do raw sockets, it’s outdated and all new APIS in streams and filesystem functions are replacements for the extension.

Process work like a stream. They are Black Magic

  • pipes
  • STDIN,STDOUT,STDERR
  • proc_open
  • popen

Stream contexts are how you tell your streams how to behave (parameters and options).

PHP 5.3 has built in streams. This is better seen in the slide, i should try to link to it later. We have a magic variable ($http_response_headers) to get headers when using http.  When you’re doing a POST, you still use file_get_contents. Extensions allow you to talk raw ssl as well as ssh,phar,zlib,bzip.

An example for sockets uses the exact way that paypal tells you how to do it.

Built-in filters

Generally useless but provided by php.

  • string filters
    • string.rot13
    • string.toupper
    • string.tolower
    • string.strip_tags
  • dechunk
    • decode remote HTTP chunked encoding streams
  • consumed (eats data and that’s all it does)
Reblog this post [with Zemanta]

2 comments on “tekx – streams, sockets and filtersAdd yours →

Leave a Reply