Anewt overview

This chapter gives an overview of Anewt, explaining the basic structure and the modular architecture and how to use Anewt in your own projects.

Basic structure

Anewt is a package of several modules, each providing specific functionality, bundled together with some glue code. Some modules depend on other modules, but you usually don't have to worry about this. Dependencies will be automatically pulled in by the module requiring it.

Each module may contain multiple files, classes and functions. Each filename ends with a .lib.php suffix. Most likely the classes in a module depend on each other. For instance, the DB class depends on the PreparedQuery and ResultSet classes.

Bootstrapping Anewt

To use Anewt in your code, you should include the file anewt.lib.php. You don't need to fiddle with include paths, directory layouts or do other tricks. As you can see, almost no effort:

Example 1: Initializing Anewt

require_once '/path/to/anewt/anewt.lib.php';

Keep the Anewt libraries in a directory outside your DocumentRoot. The same goes for your own include files and libraries. This way you don't expose unneeded stuff to the world, so that malicious people and search engines cannot visit the places you don't want them to have access to.

Initializing Anewt is extremely non-intrusive. You can continue to work with your code exactly like you did before, except for one thing: if needed, GET, POST and cookie values are unquoted. Since the value of this php.ini setting differs wildly on many hosting setups and you want to make sure you're using a sane environment, deobfuscating these input variables is extremely important. The initialization routines apply stripslashes() recursively on the the $_GET, $_POST, $_COOKIE variables, but only if magic_quotes_gpc was turned on in your PHP configuration. Furthermore, set_magic_quotes_runtime(0) is called to make PHP not screw up data resulting from some I/O functions. No thanks PHP, we know perfectly well which data needs escaping!

Initialize Anewt as soon as possible by putting the require_once '/path/to/anewt/anewt.lib.php' call at the top of your initialization files.

Loading modules

Loading an Anewt module means making the functionality the module provides available to your code. In practice this means that the classes in the module are loaded and maybe some initialization routines are performed, such as defining some constants or detecting some environment settings and acting upon them. After the module is loaded and your code can start using the functionality.

To load a module, the anewt_include() function is used. You can pass one or multiple module names as parameters to this function.

Example 2: Loading Anewt modules

// Load some modules
anewt_include('database');
anewt_include('xhtml');
anewt_include('textformatting');
 
// Load a submodule. Some modules do not load all
// their functionality by default. Make sure to
// load the parent module first!
anewt_include('validators');
anewt_include('validators/nl');
anewt_include('validators/nl/dutchzipcodevalidator');

Not all modules provide all their functionality (classes, functions) after loading them using the anewt_include() function. Some modules contain submodules. These can be loaded by specifiying the submodule name after a backslash: anewt_include('modulename/submodulename'), as can be seen in the example above. Not loading all functionality offered by a module means less overhead for your application. Why load classes you're not even using?