O'Reilly logo

Upgrading to PHP 5 by Adam Trachtenberg

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required