Skip to main content

Posts

Showing posts from May, 2015

Up to Speed with PHP: Choosing your Quotation Marks Carefully

In PHP we typically see two ways of representing a literal string value, one using single quotation marks
$str = 'this is a string'; and another using double quotation marks.
$str = "this is a string"; And the first thing to note is that if you use single quotation marks within a string value that opens and closes with single quotation marks then you must escape these,
$str = 'this is a \'string\''; whereas if you use double quotation marks within single quotation marks then you do not escape them,
$str = 'this is a "string"'; and vice versa when using double quotation marks to enclose a string.
$str = "this is a \"string\""; $str = "this is a 'string'"; But the differences between single and double quotation marks are more than skin deep. The main difference is that single quotations interpret most things literally
$value = 'Hello PHP!'; $str  = '$value'; echo $str; // outputs: $valu…

Revisiting PHP: Connect to a Database

A few years back I wrote some PHP code in rather a hurry and grabbed chunks of code from here there and everywhere. Now going back to it, I want to better understand the code and to refine it. At the same time checking that it remains an acceptable way of doing things and hasn't been outdated.
Going PDO The first challenge is to create a class (following on from yesterday's post) that will connect to a database. In order to do this I use a PDO (or PHP Data Object) to connect to a MySQL database:
class Connect { public $dbh; private $error; static function dsn($type,$db,$host,$port) { return $type.':dbname='.$db.';host='.$host.';port='.$port; } function __construct($dsn,$user,$password) { try { $this->dbh = new PDO($dsn, $user, $password); } catch (PDOException $e) { $this->error=$e->getMessage(); exit('Not able to connect to database. Error message: '.$t…

Up to Speed with PHP: Classes, Constructors, Static Methods, Extensions and more

This is a brief post, outlining some of the essentials of working with classes in PHP.
Classes A class in PHP is created very simply like this:
class MyClass { } and is instantiated using the following syntax:
$classInstance = new MyClass(); ConstructorsConstructors are called when a class is instantiated. It is a method that is included under the umbrella term magic methods. These methods are identified by their double underscore prefix and are called automagically by the system.
class MyClass { function __construct() { echo "Hello World!"; } } Variables If we want to set variables then a constructor is the place to do it:
class MyClass { public $var; function __construct($var) { $this->var = $var; echo $var; } } Variables can be public, private or protected.
"Class members declared public can be accessed everywhere. Members declared protected can be accessed only within the class itself and by inherited and parent c…

Swift and NSScanner: Coping with Cocoa

An NSScanner is instantiated with a String and once instantiated it can be searched. This searching occurs via a stepping through of the string. For example, let's suppose we have the following string, "Swift 1.2", and want to set up a scan for the letters in this string:
// first we create an NSScanner instance let scanner = NSScanner(string: "Swift 1.2") // then we create a string into which the scan will output var str:NSString? // finally we perform the scan scanner.scanCharactersFromSet(NSCharacterSet.letterCharacterSet(), intoString: &str) // true // and here we have the result str // "Swift" Moving on But suppose we ran the same scan again:
scanner.scanCharactersFromSet(NSCharacterSet.letterCharacterSet(), intoString: &str) // false scanner.string // "Swift 1.2" This time the result is false, not because the string has changed or been consumed but because the scanner has moved forwards, as we see when we examine the .scanLoc…

Adventures in PDF: Swift and PDFKit

Working with PDFs in OS X is straightforward using PDFKit (update: and in iOS since iOS 11).
PDFDocument It all starts with the PDFDocument class and in order to leverage this you simply import Quartz and get started. Like so:
import Quartz let url = NSBundle.mainBundle().URLForResource("myPDF", withExtension: "pdf") let pdf = PDFDocument(URL: url) pdf.pageCount() // number of pages in document pdf.string() // entire text of document As you can see counting the number of pages and accessing the text is straightforward and there's plenty more to explore alongside this, but I want to move quickly along to working with pages.
PDFPage While you can access a string of the entire document and this is useful for searches, more often you'll want to work with PDFPage instances. You access pages by their index:
import Quartz let url = NSBundle.mainBundle().URLForResource("myPDF", withExtension: "pdf") let pdf = PDFDocument(URL: url) let page = pd…

JSON and Swift: Making the Bool magic happen in Aldwych (updated with NSNumber extension)

Aldwych is a JSON parsing repo I've written for Swift, which is hosted on GitHub.
The problem NSJSONSerialization can write Bool values but it imports them as NSNumber values of zero or one, representing false and true respectively. The problem is further exacerbated by the fact that all NSNumbers can be cast to Bool and all Bool values can be cast to NSNumber. This makes it very difficult to distinguish between them.
The solution An NSNumber created from a Bool does retain enough knowledge about itself, however, to know that it is equal in value and type to an NSNumber instantiated with a Bool of the same kind. This is thanks to its inheritance from NSValue. So while the following will return true,
NSNumber(bool: true).isEqualToNumber(NSNumber(integer: 1)) // true if we instead use the NSValue method isEqualToValue(),
NSNumber(bool: true).isEqualToValue(NSNumber(integer: 1)) // false false will be returned. This enables the following code to be written:
else if let v = v as? NSNu…

Fitness Apps: Week 18 (Google Fit, MyFitnessPal and MapMyRun)

It's been a good number of weeks since I last bored you with my experience of using fitness apps. So I thought it time to inflict a little more tedium upon you since there were a couple of landmarks this week: I bought my first pair of new exercising trainers in over a decade and I ran my first 15 km route.
Newwww shoooes To explain, when I began running at the beginning of the year I simply pulled out my last pair of trainers and glued the soles back to the body and off I went. A zero cost beginning. It was fine, they've chugged along nicely for an elderly pair of trainers but finally it was time to admit there was only so much gluing I could do and so I bought my first pair of proper running shoes, ever!

Nothing extravagant or flashy but I'm really pleased with them, they fit snugly and give a bounce that's been missing. (For those interested, they are a pair of Adidas Duramo 6 trainers. I decided to stick with Adidas because that's what I had before and they'…