Many file formats contain data in a packed manner: a series of values which encode information, placed into the file one after another. As an example, TrueType font files encode information about the font represented in the file, such as the name of the font and how many characters are contained within the font face. As another example, Windows BMP files encode the dimensions and formatting of the image within the format.
The Need: Structures in C/C++
In most instances, the information in a file is encoded as a series of
structures, grouping related information such that a programming interface
can retrieve them easily. In C and C++, a special type is set aside for
just such a reason: the
BMP header structure, in C
The above is the C representation of one palette entry in a Windows BMP.
Once this structure has been defined as a type with
using it is very simple:
Using a structure, in C
It can be seen above that the data contained within a
can be accessed in much the same way as methods can be accessed within a class,
in C++ or any other object-oriented programming language. Indeed, in C++ the
struct are equivalent, and mean
much the same thing.
The Problem: Structures in PHP
When using languages such as PHP, a problem arises: PHP does not support a
struct type. Further, since PHP is a loosely-typed
language, it's not possible to read data from an encoded file format directly
into PHP and manipulate it. This can be alleviated by using PHP's
keyword to build a class containing the structure members:
A PHP class with structure members
This representation of the structure makes use of the
function, to take a string of binary data and load it into the class members.
This can be used in a similar fashion to the C representation:
Using a structure, in PHP
This approach has distinct advantages over the C
In particular, the PHP implementation is a class, and can contain methods
other than the simple constructor; furthermore, complex types can be contained
within the structure in a way that C cannot accomplish.
Advanced Usage: TrueType File Format
An example of complex usage of the structure pattern is in parsing of the TrueType file format. A TrueType file defines a vector or bitmap font face, and contains a series of data tables: a table of names, a table of Windows-specific font information, and tables of glyph definitions. Also contained in the file structure is a header defining the table "directory", which allows a parser to find these tables within the file.
A TrueType file begins with information about the version of the TrueType specification, followed by the table directory. This can be represented in PHP in a simple manner:
TrueType file header and table directory, in PHP
This example shows how it's possible for a structure to contain more
information than the sum of its members. In the case of the
ttfHeader, the constructor can pull in the structure of an
entry in the table directory, and build its own array to represent the
All in all, the Structure pattern makes it possible for PHP to represent
structures in a similar fashion to C
structs; it also allows
PHP to be more versatile, and represent more complex information related to
the data, which would normally have to be held outside the structure.
© Imran Nazar <email@example.com>, 2008
Article dated: 20th Jul 2008