Many websites use databases for storing data for use by web applications. There are situations in which this is not the best solution, however. For light, moderate, or temporary storage requirements, you might prefer to use flat files, session variables, and cookies. This article will describe how to create a class that handle all of these. It is the first of two parts.
If the mode is set to "f" then we will be storing the data in a flat file. All the usual permissions issues and security precautions are in force here, and the file may not be created if they aren't followed.
You may have noticed that I haven't mentioned the third and final argument to the constructor. Until now it didn't have any purpose; now it does.
The dir argument sets an optional directory for the files that datastore uses. The various if/else blocks in this code take care of determining whether the default directory or a user specified one should be used and creating that directory if it doesn't yet exist. The default directory is called "data" and resides in the same directory as the datastore class regardless of the file that includes it. This is accomplished using the magic constant "__FILE__". This built-in PHP constant returns the path to the file, unlike $PHP_SELF which always refers to the master page. Any includes lose their selfness when they are included by another file.
The cookie code is something we will deal with later. For now, this is the constructor method in its entirety.