PhpRiot
Download This Article
Download this article in PDF format with all listings and files.

Price: $5.00 AUD
(Approx. $4.10 USD)

More information
Related Books
Professional Linux Programming

Professional Linux Programming

As Linux increases its presence throughout the world as a target platform for professional...
Browse Articles
Ajax (4), APC (1), CAPTCHA (1), CSS (3), Debugging (1), File Upload (1), Google (3), Google Maps (2), JavaScript (12), JSON (2), MVC (1), MySQL (7), onbeforeunload (1), OOP (1), PHP (28), PhpDoc (1), PostgreSQL (6), Prototype (11), Reflection (1), RFC 1867 (1), Robots (1), Scriptaculous (1), SEO (1), Sessions (1), SimpleXML (1), Smarty (5), SOAP (1), SPL (1), Templates (2), W3C (1), XHTML (1), Zend Framework (1), Zend_Search_Lucene (1)

PhpRiot Newsletter
Your Email Address:

Managing Your Data With DatabaseObject

Setters, Getters And Checkers

For every different column in your table, it is possible to define three separate callback functions.

Setters

A setter callback is used to manipulate a value or perform some action when someone tries to set a value, using the setProperty() method.

For example, let’s say when a user’s username is set, internally you want to store some extra characters with that username. So let’s say a person specified fred as their username, but you wanted to store it as fred@phpriot instead, you could create a setter which did this for you.

Setters have the function name of _setCOLUMNNAME. They are automatically called when that field is set. A setter takes the value the user tried to set, and returns the new value. So to implement our example from the previous paragraph, the setter would look like:

Listing 2 listing-2.php
<?php
    class User extends DatabaseObject
    {
        /* ... other code ... */
 
        function _setUsername($username)
        {
            return $username . '@phpriot';
        }
    }
 
    $user = new User($db);
    $user->setProperty('username', 'fred');
?>

If a username contains an underscore, such as first_name, the setter would be called _setFirst_name().

Additionally, the passed in value must pass the corresponding checker method if one exists (see further below).

Getters

A getter callback is used to manipulate a value someone is trying to fetch from the active record, using the getProperty() method. So as with the setter example, if you store the username as fred@phpriot, you might want to only return the fred part to the user, as that is what they think their username is. Getters work opposite to setters, so the stored value is passed in, and the value to send back is what is returned. Additionally, getter methods are prefixed with _get.

Listing 3 listing-3.php
<?php
    class User extends DatabaseObject
    {
        /* ... other code ... */
 
        function _getUsername($username)
        {
            return preg_replace('/@phpriot$/', '', $username);
        }
    }
 
    $user = new User($db);
    echo $user->getProperty('username');
?>

Checkers

A checker callback is used to ensure the passed in value is valid. It is called either when the user called setProperty() or checkProperty() on the respective field.

Checkers return true if the value is deemed to valid, or false if not.

Going back to our user example, you might want to ensure the user enters a valid email address, which you could do with the following:

Listing 4 listing-4.php
<?php
    class User extends DatabaseObject
    {
        /* ... other code ... */
 
        function _checkEmail($email)
        {
            return preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$/i', $email);
        }
    }
 
    $user = new User($db);
    if (!$user->checkProperty('email', 'fred@example.com')) {
        echo 'Invalid email address';
    }
?>

In This Article


Tagged in , ,

Additional Files