What is the PHP equivalent of the VB class stream?
As discribed on this MSDN page
What would be the PHP equivalent?
Is it even possible to do the three things listed on that page under remarks
Streams involve three fundamental operations:
You can read from streams. Reading is the transfer of data from a stream into a data structure, such as an array of bytes.
You can write to streams. Writing is the transfer of data from a data structure into a stream.
Streams can support seeking. Seeking is the querying and modifying of the current position within a stream. Seek capability depends on the kind of backing store a stream has. For example, network streams have no unified concept of a current position, and therefore typically do not support seeking.
PHP does not have a generic class to deal with this, it has a data type that you may not be used to working with - the Resource.
Resources can be used to hold file descriptors for streams. They are used in a wide variety of places, including file system descriptors, network streams, maintaining database connections and even database result sets. They are also used to maintain context information, this type of resource is not a stream but behaves more like a data store. It is not possible to define your own resource types with PHP alone, they are a part of the underlying C++ code.
In many ways resources behave like an instance of an object, and it's probably best to think of them in this manner (although they cannot be serialized or passed around between processes like objects). It is important to note, however, that PHP is not an object oriented language - it is a scripting language that provides many OO features, and forget that at your peril. The functionality of the methods provided by system.io.stream is provided in PHP through procedural functions that accept a stream resource as an argument, and exactly which functions you should use depend on the type of stream that you are using.
Most stream resource types (but not all - a notable exception is those created by the sockets extension) can be read from and written to by fread(), fwrite() et al. The streams "extension" (enabled by default in all new PHP installations for a very long time) attempts to provide a generic layer to create, access and operate upon streams of various different types, but it has it's limitations:
- Some functionality does not work with streams that point to the standard file descriptors or an external process on Windows (see stream_select())
- While the streams extension provides much of the functionality that the sockets extension does, it does not provide all of it (e.g. multicast client)
- Due to the single-threaded synchronous nature of PHP, non-blocking multi-IO is a complete PITA to implement. This is not a limitation of streams but of PHP itself.
- [Other limitations that have bitten me in the past, none of which I can think of at the moment]
If you want to deal with streams in a properly OO manner, you will have to write a class that wraps the procedural function calls.
What about PHP streams? They support pretty much exactly what you seem to be looking for.