27
Jun '12

When pulling apart other people’s code, all too often I come across occasions where people are using up resources creating loads of instances of objects where one would do just as well. So here is the quickest way to use static object instances that can be accessed from anywhere (apologies if I’m teaching anyone to suck eggs).

class MyClass {

	protected static $instance;

	protected function __construct() {
	}

	public static function getInstance() {
		if(!self::$instance)
			self::$instance = new self();

		return self::$instance;
	}
}

And there you have it! Just call it like so and you never need to spank your resources in such a manner again!

$instance = MyClass::getInstance();
14
Jun '12

I have no problems using jQuery plugins that are rather old, if they are well written and efficient for my purpose. However jQuery hasn’t maintained complete backwards-compatibility throughout it’s life, notably when in version 1.5 the ‘handleError’ function was removed.
It is very simple to put in a patch for older plugins that still use this function – just extend jQuery and add the function back in yourself. If you don’t care about the error messages you can just add an empty function block but I prefer to use something like this to give me a better clue about the errors I might be dealing with:

jQuery.extend({
	handleError: function( s, xhr, status, e ) {
		// If a local callback was specified, fire it
		if ( s.error )
			s.error( xhr, status, e );
		// If we have some XML response text (e.g. from an AJAX call) then log it in the console
		else if(xhr.responseText)
			console.log(xhr.responseText);
	}
});