A list of commands I’ve used over the years.
Since I want to implement QR codes into the URL shortener, I wrote a function in PHP that generates the link usin’ Google API.
/** * Class for QR Codes * */ class QR { /** * Function returns URL to Google API QR Generated image * * @param url - The URL to be turned to QR Code * @param size - Size of the QR image * @param errorCorrection - Amount of reduntant information the QR code has (L - default, M, Q, H) * @return Google API QR Generated image (to be used directly in img tag) */ public static function getQRforURL($url, $size, $errorCorrection = "L") { $link = "https://chart.googleapis.com/chart?" . "cht=qr" . "&" . "chs=$size" . "x$size" . "&" . "chl=" . urlencode($url) . "&" . "choe=UTF-8" . "&" . "chld=$errorCorrection"; return $link; } }
Design patterns are reusable solutions to common software engineering problems. They are best-practice in software design. It is not easy to apply them, and it often happens that pattern misuse may lead to bigger problems, a good design supported by patterns will last and give you less headaches. Examples in PHP and Java are given.
Singleton is a creational pattern (creational patterns deal with object creation mechanisms) which restricts instantiation of a class to one (and only one) object. We’ll discuss it in 3 chapters:
Since it’s needed for URL Shorteners, and that’s what I’m writing a tutorial about, here’s a number converter from decimal, to whatever base you need (if you supply a base array long enough). Written in PHP.
Update (2011-07-27):
Changed method to static and added an exception.
/** * Class with static methods for converting numbers from decimal to a given base number system * */ class NumberConverter { /** * Array to be used for conversion */ private static $base = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+="; /** * Get's the $number character from $base array * * @param number - the position of the character needed * @return Returns a character from $base for a given $number */ private static function getCharacter($number) { return self::$base{$number}; } /** * Converts a decimal number to a given base * * @param number - Number to be converted * @param toBase - Number base to be converted to * @throws Exception */ public static function fromDecimalToBase($number, $toBase){ // Check if the base is greater then the max possible base if ($toBase > strlen(self::$base)) { throw new Exception('Base too big.'); } // Convert number to integer (better safe than sorry) $number = intval($number); // Set a flag if the number is negative, do a positive conversion then just add the minus sign $negative = false; if ($number <= 0) { $negative = true; $number = $number * -1; } $converted = ""; while ($number != 0) { // Get a number in the new base $modulus = $number % $toBase; // Get the number representation in that base $converted = self::getCharacter($modulus) . $converted; // Remove the extracted number and continue until the number is 0 $number = intval(($number - $modulus) / $toBase); } // Add the minus sign if it was negative if ($negative) { $converted = "-" . $converted; } return $converted; } }