UltraMega Blog

Creating A Database Connection On Demand

It may be useful to only create a database connection when you actually use it for the sake of efficiency. Here is a wrapper for MySQLi that does just that in the most simple way I could devise. A (possibly desired) side-effect is that this limits you to a single connection.

How you pass in the connection details is up to you. You can hard-code them into the mysqli initialization, put them in class constants, or pass them into a constructor that sets static properties.

 * Wrapper for MySQLi
 * Creates a database connection on demand
class DB {
    private static $db;
    private function connect() {
        if(!isset(self::$db)) {
            self::$db = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
    public function __call($name, $arguments) {
        return call_user_func_array(array(self::$db, $name), $arguments);
    public function __get($name) {
        return self::$db->$name;
    public function __set($name, $value) {
        self::$db->$name = $value;
    public function __isset($name) {
        return isset(self::$db->$name);
    public function __unset($name) {


$db = new DB(); // functionally identical to a mysqli object
Tagged as: , , , No Comments

Understanding and Applying Polymorphism in PHP

Understanding and Applying Polymorphism in PHP | Nettuts+

In object oriented programming, polymorphism is a powerful and fundamental tool. It can be used to create a more organic flow in your application. This tutorial will describe the general concept of polymorphism, and how it can easily be deployed in PHP.

Read full tutorial at Nettuts+


Create Callbacks Using __invoke

The __invoke magic method (available as of PHP 5.3) is used to make an object callable as if it were a regular function. The combines the usefulness of objects with the simplicity of functions. One application of this feature is creating callback functions that can be passed around like objects. This is common practice in other languages like JavaScript, and can be very useful. This tutorial will show you how to create a callback function using the __invoke method.


Deciphering Magic Methods in PHP

Deciphering Magic Methods in PHP | Nettuts+

PHP provides a number of ‘magic’ methods that allow you to do some pretty neat tricks in object oriented programming. These methods, identified by a two underscore prefix (__), function as interceptors that are automatically called when certain conditions are met. Magic methods provide some extremely useful functionality, and this tutorial will demonstrate each method’s use.

Read full tutorial at Nettuts+


Snippet: Maintain a Single Database Object in PHP 5 Using the Singleton Pattern

When creating a PHP application, it is usually necessary to connect to a database to perform certain tasks. In some cases you only want to open a connection when necessary, but limit it to a single connection. This way you don't waste resources on unnecessary database connections. For these situations I use the Singleton Pattern, which is perfect for this.

In this example, we are creating a MySQLi object and forcing it to a single instance. We just need to call DB::get() to create and/or access the object.


What’s new in PHP 5.3?

With last week's announcement of RC4, the final release of PHP 5.3 is imminent. So what can we expect in this version? Here I'll outline the biggest and most interesting changes and additions. This is definitely not exhaustive since there are tons of minor changes and improvements in this version, but these are some of the ones I found interesting.

Tagged as: , Continue reading