UltraMega Blog
1Sep/110

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.

<?php
/**
 * 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) {
        $this->connect();
        return call_user_func_array(array(self::$db, $name), $arguments);
    }
    public function __get($name) {
        $this->connect();
        return self::$db->$name;
    }
    public function __set($name, $value) {
        $this->connect();
        self::$db->$name = $value;
    }
    public function __isset($name) {
        $this->connect();
        return isset(self::$db->$name);
    }
    public function __unset($name) {
        $this->connect();
        unset(self::$db->$name);
    }
}

Usage:

<?php
$db = new DB(); // functionally identical to a mysqli object
$db->query('...');
Tagged as: , , , No Comments
29Nov/100

Creating a Twitter OAuth Application

Creating a Twitter OAuth Application | Nettuts+

OAuth can be a tricky concept to wrap your head around at first, but with the Twitter API now requiring its use, it is something you need to understand before creating a Twitter application. This tutorial will introduce you to OAuth, and walk you through the process of creating a basic application.

Read full tutorial at Nettuts+

11Oct/1086

Create an Upload Progress Bar With PHP and jQuery

When it comes to uploading files, users expect visual feedback, usually in the form of a progress bar. The problem is that PHP doesn’t offer a way to track file uploads in progress by default. Fortunately, there is an extension that enables this functionality and this tutorial will show how it can be combined with jQuery UI to create a progress bar.

Here is a demo of the effect we will be building in this tutorial:

9Sep/100

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+

1Sep/101

Record HTML Canvas Animations to Video

Sometimes it might be useful to be able to record your canvas animation to a video format. Maybe you want to use your JavaScript skills to create fancy effects for a video. You could use some kind of screen capturing program and crop the video, but I'll show you how to do it with code!

Note: You'll need an HTTP server with PHP running on your local machine to do this. Don't try this with your website over the Internet, unless you don't mind waiting for tons of images to upload... Also, I recommend using Chrome for best results.

Let's start with our simple animation. You can see it in action here.

27Jul/100

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.