Creating Wrappers

You can write custom wrappers in PHP. This lets you extend PHP to “speak” any protocol you want while still letting you use the standard stream functions, such as file_get_contents( ).

Stream wrappers are implemented as a class with specific methods. These methods are called by the stream-handling code when a stream using your wrapper is opened, read from, written to, and so forth.

In all, there are 17 potential methods that make up a complete wrapper, but it’s rare to actually code all of these. Table 8-2 contains a list of wrapper methods and when they’re evoked.

Table 8-2. Wrapper methods

Method

Function

PHP 5 only?

boolean stream_open(string path, string mode, int options, string opened_path)

fopen( )

No

void stream_close(void)

fclose( )

No

string stream_read(int count)

fread( ), fgets( )

No

int stream_write(string data)

fwrite( ), fputs( )

No

boolean stream_eof(void)

feof( )

No

int stream_tell(void)

ftell( )

No

boolean stream_seek(int offset, int whence)

fseek( )

No

boolean stream_flush(void)

fflush( )

No

array stream_stat(void)

fstat( )

No

boolean unlink(string path)

unlink( )

Yes

boolean mkdir(string path, int mode, int options)

mkdir( )

Yes

boolean rmdir(string path, int options)

rmdir( )

Yes

boolean dir_opendir(string path, int options)

opendir( )

No

string dir_readdir(void)

readdir( )

No

boolean dir_rewinddir(void)

rewinddir( )

No

Get Upgrading to PHP 5 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.