Home arrow PHP arrow Page 3 - The Data Mapper Design Pattern: A Final Example

Building a simple autoloader class - PHP

Welcome to the final part of a series on implementing the Data Mapper design pattern in PHP 5. This series teaches you the key concepts that surround the development of data mapper classes. It complements the corresponding theory with a decent variety of code samples.

  1. The Data Mapper Design Pattern: A Final Example
  2. Review: the earlier domain object and the data mapper classes
  3. Building a simple autoloader class
  4. Putting the user mapper class to work
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
April 07, 2010

print this article



To simplify the code required to demonstrate the use of the previous user mapper class, in this section I’m going to define a basic autoloader. It will permit you to include all of the classes shown before through a unique, centralized point.

The logic implemented by the autoloader is very trivial; it looks like this: 







// register default autoloader class

spl_autoload_register(array('Autoloader', 'autoload'));



// define class 'Autoloader'

class Autoloader


    public static function autoload($class)


        if (class_exists($class, FALSE))


            return TRUE;


        $file = $class . '.php';

        if (!file_exists($file))


            require_once 'ClassNotFoundException.php';

            throw new ClassNotFoundException('The file containing the requested class was not found.');


        require_once $file;


        if (!class_exists($class, FALSE))


            require_once 'ClassNotFoundException.php';

            throw new ClassNotFoundException('The requested class was not found.');




If you’ve ever developed autoloader classes before, you’ll find the definition of the above one easy to follow. It behaves like a basic wrapper for the “require_once” PHP construct. In addition, you can see that the autoloader will throw a custom exception at the appropriate places when a requested file or class is not found. So, below I included the file that contains this user-defined exception class:







class ClassNotFoundException extends Exception{}

Now that there exists an autoloader that will lazy load all the sample classes  listed in the preceding segment, we're finally ready to put the previous user mapper class into action. Therefore, in the last segment I’m going to code a script that will show how to use the mapper to translate data stored in a MySQL table into user domain objects, and vice versa.

To understand the details of this process, read the following section.

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: