Perlessence

Blog


daonut 0.1

March 24, 2008 20:51 Web Development PHP

daonut 0.1 is out! As of this build, you can create a MySQL database connection, correlate databases to DSN aliases and DSN aliases to database connection strings, and pass through raw SQL.

For version 0.2:


RSS Feeds with daonut

February 12, 2008 08:29 Web Development PHP

I've recently started work on a database abstraction layer that I call daonut. It's both a learning project as well as a useful utility that I need for building database applications. Ok, I guess I don't need it, but I'm lazy these days and it's easier to make an abstracted call than to manually open the MySQL connection, build and send a query, test for errors, etc. daonut was written based on similar syntax to Gaia's database layer, but with some of the calls tweaked to match my preferences. I didn't look at any of Gaia's code, partially for a learning experience, and partially so I could avoid any copyright issues. I'll have a more detailed post on daonut later, but I wanted to put up a couple examples of the syntax to (hopefully) whet your appetite for more.

include_once '/path/to/daonut/daonut.inc.php';
/* First it's an RSS feed from the internet */
DaoFactory::connect('RSS');
$dao = DaoFactory::create('Feeds.Corkd'); // My Cork'd cellar feed
$dao->execute();
echo "Total rows : " . $dao->affectedrows();
while ($row = $dao->fetchrow()) {
    var_dump($row);
}

/* Now it's a MySQL database */
DaoFactory::connect('MySQL', 'localhost', 'username', 'password');
$dao = DaoFactory::create('Feeds.Corkd'); // table 'corkd' on database 'feeds'
$dao->execute();
echo "Total rows : " . $dao->affectedrows();
while ($row = $dao->fetchrow()) {
    var_dump($row);
}

Handling arrays in a static class

June 17, 2007 00:42 Web Development PHP

I'm creating a class to manage layout configuration on my site. It does things like set the page title, site name, includes JavaScript and CSS files, etc. This is modeled after Gaia's LayoutManager class, but much simpler because it doesn't have Gaia's site-specific stuff in it. Like Gaia's LayoutManager, it is a static class, all public methods and properties are static; this allows the class to manage site stuff without carrying an instance around. However, there is one big problem: PHP evidently doesn't know how to handle class properties that are arrays when called in a static context.

Finally, I solved the problem using a variation on Gaia's solution. I created a separate data container class that holds all the information. Then, each setter calls a method in the LayoutManager class that checks if a static class property is an instance of the LayoutManagerContainer and if not, creates an instance of the container class and stores it in the static class property; either way, the container object is returned. Then I can access the properties I want directly within the class. To get the data container values from outside the LayoutManager, I wrote a getter method that takes a parameter name and returns the appropriate value from the stored data container.

class Foo {

    private static $data;

    public static function addToArray($var) {
        $data = self::data();
        $data->my_array[] = $var;
    }

    private function data() {
        if (!is_a(self::$data, 'FooContainer')) {
            self::$data = new FooContainer();
        }
        return self::$data;
    }

    public static function get($param) {
        $data = self::data();
        if (property_exists(self::$data, $var)) {
            return $data->$var;
        }
    }

}

class FooContainer {
    public my_array = array();
}

Foo::addToArray('hello');
Foo::addToArray('goodbye');
$data = Foo::get('my_array'); // $data = array('hello', 'goodbye');
© Probably a million other people from whom I've subconsciously stolen this layout. Also, Karen Ziv (me). Don't steal my stuff.