Hi. You've reached Nigel.in
This site is still in its very early stages, but what you have landed on, possibly inadvertently, is the website of a certain curious character a.k.a Nigel Fernandes
The aim of this page was to serve as a landing point for those of you who want to know a little more about me. The links on the right and left will take you deeper my world, or possibly on to different exciting things.
Its a big web out there and this is just one more street for you to saunter down. I hope you like it.
Close this.
About Me

- Name: Nigel Fernandes
- Location: Panjim, Goa, India
Crazy, dancing, programming, Goan.. I'm a computer geek and proud to be one. I program in Java, Ruby and .Net, PHP, Javascript. A lot of my recent work has been about CSS and UI design practices for large scale websites and Agile teams. I still while away hours dreaming up a web startup.
Implementing the Observer Design Pattern with PHP
Saturday, March 01, 2008
If you, like me need to implement a light weight Observer framework within your PHP project, and really do not need PEAR's Event_Dispatcher, look no further. ;-)Below is my light weight implementation of a Observer pattern for PHP.
interface Observerable {
public function attach(Observer $observer);
public function detach(Observer $observer);
public function notify();
}
interface Observer {
public function update(Payload $payload);
}
class Payload {
public $data;
public function __construct($data) {
$this->data = $data;
}
}
class Watched implements Observerable {
private $state;
private $watchers;
public function __construct(){
$this->watchers = Array();
}
public function attach (Observer $observer) {
if (array_search($observer, $this->watchers, true) === false) {
$this->watchers[] = $observer;
}
}
public function detach (Observer $observer) {
$observerOffset = array_search($observer, $this->watchers, true);
if ($observerOffset !== false) {
unset($this->watchers[$observerOffset]);
}
}
public function notify () {
foreach ($this->watchers as $observer) {
$observer->update(new Payload($this->state));
}
}
public function changeState($newState) {
$this->state = $newState;
$this->notify();
}
}
class Watcher implements Observer {
public function update (Payload $payload) {
echo $payload->data;
}
}
// Create Objects
$watched = new Watched();
$watcher1 = new Watcher();
$watcher2 = new Watcher();
// Attach Watchers
$watched->attach($watcher1);
$watched->attach($watcher2);
// Change State of Watched
$state = "foo";
$watched->changeState($state); // Causes "foo" to be printed from within Watcher1 & Watcher2
// Remove a Watcher
$watched->detach($watcher1);
$watched->changeState($state); // Causes "foo" to be printed from within Watcher1
I tend to use a slightly different naming system for most my classes. Apart from that, this small snippet of code is pretty much a standard Observer pattern implementation. Very useful when you are writing small scale PHP applications (like a Facebook App. for example).I also tend to favor the use of a small container class for passing data into the watchers, but of course since this is PHP, you could probably just use a complex array instead.
Labels: Design Patterns, Facebook PHP application, Observer Pattern, PHP
Dynamic Javascript source includes in XUL Applications and Firefox Extenstions
Friday, February 29, 2008
So you want to load a snippet of JavaScript code dynamically from the context of a script executing in your FireFox Application.The cleanest way to achieve this is to use the rather little known mozIJSSubScriptLoader interface. You can see the documentation for the interface here.
The beauty of this method is that you can load the snippet or code block of JavaScript into a defined scope. Neat! no messy globals or eval() calls.
For example:
Lets say that you have a file test.js that can be accessed at the Chrome path chrome://yourextensionname/content/test.js and it contains the following:
function Foo () {Lets say you want to load this test.js file into your application. I suggest creating a function that can load this code into a defined scope as below:
this.baz = "deep value";
}
Foo.protoype = {
readBaz : function () {
alert(this.baz);
}
}
function includeJS(chromeFilePath, NameSpaceContainer) {You could then use this includeJS() as show below:
var scriptLoader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"]
.getService(Components.interfaces.mozIJSSubScriptLoader);
scriptLoader.loadSubScript(chromeFilePath, NameSpaceContainer);
}
var baseURI = "chrome://yourextensionname/";
var MyApplicationNameSpace = {};
try {
includeJS(baseURI+"content/test.js",MyApplicationNameSpace);
} catch (e) {
alert("Could not load Namespace: Encountered excpetion "+e);
}
var myFooObj = new MyApplicationNameSpace.Foo();
myFooObj.readBaz(); // will alert "deep value"
As great as this method of loading Javascript is, there are other ways you could consider. You could for instance use Mozilla's io-service coupled with an eval() as below:
function includeJS(chromeFilePath, NameSpaceContainer) {The problem with both these two methods is that they break when you try to load a Javascript source file which contains special Unicode characters. As a work around for this I suggest using a
var IOService = Components.classes["@mozilla.org/network/io-service;1"]
.getService(Components.interfaces.nsIIOService);
var IOStream = Components.classes["@mozilla.org/scriptableinputstream;1"]
.getService(Components.interfaces.nsIScriptableInputStream);
var data = IOService.newChannel(chromeFilePath, null, null).open();
IOStream.init(data);
var dataStr = IOStream.read(data.available());
IOStream.close();
data.close();
eval(dataStr, NameSpaceContainer);
}
technique borrowed from buzzword technology AJAX ;-) , and do a XmlHttpRequest call to a Chrome URL.
For example you could use the following code as replacement to the two previous includeJS() methods:
function includeJS(chromeFilePath, NameSpaceContainer) {
var httpRequest = new XMLHttpRequest();
httpRequest.onload = function() {
eval(this.responseText,NameSpaceContainer);
};
httpRequest.open("GET",chromeFilePath, true);
httpRequest.send(null);
}This seems to work even for source files that contain funky Unicode characters. You could replace the Chrome URl with file paths relative to your system, but I'd recommend always using a Chrome URL where possible. Its a better, more file system agnostic approach.Labels: Javascript, Javascript quality, Mozilla, XUL
Interesting PHP function scoping caveat
While playing around a personal PHP project the other day I stumbled across some rather unsual PHP scope scenario. I was in the process of migrating some instance methods to static methods when I discovered the following:Lets look at the some PHP code first:
class BarClass {
public function readData () {
echo $this->data;
}
public static function readData2 () {
echo $this->data;
}
}
class FooClass {
public $data;
public function __construct ($parameter) {
$this->data = $parameter;
}
function testScope () {
BarClass::readData();
}
function testScopeFail() {
BarClass::readData2();
}
}
$myObj= new FooClass('Spooky Scope');
$myObj->testScope(); // Will print 'Spooky Scope'
$myObj->testScopeFail(); // Will throw an error
What is so intriguing in this little snippet of code is that I'm accidentally calling the BarClass method readData() statically, yet it has the $this instance variable defined within it ...and set to the calling scope!Amazing. I wonder what kind of interesting things I could conjure up with this.
I included the other method readData2() as part of this post because a static function if properly defined will throw an error. ;-)
Labels: PHP, PHP function scope
Implementing a Singleton Design pattern in PHP
Lets say you need your PHP web application to have one and only one instance of a class at a time.A simple way to achive this is to use the Singleton design pattern.
class Singleton {
private $myPrivateAttribute ;
private function __construct ( $aParameter ) {
$this->myPrivateAttribute = $aParameter ;
}
public static function make ( $aParameter ) {
static $instance = null ;
if ($instance == null) {
$instance = new Singleton ( $aParameter ) ;
}
return $instance ;
}
public function getAttributeValue () {
return $this->myPrivateAttribute ;
}
public function __clone () {
trigger_error ( 'Clone of a Singleton is invalid.', E_USER_ERROR ) ;
}
}
The reason this results in only a single object ever being created is that the Contructor is private. Hence you cannot create more objects of the Singleton class.To verify the Singleton's make() method is infact return the same object on multiple calls try the following code:
$object = Singleton::make("original");
echo $object->getAttributeValue(); // Will print "orginal"
$secondObject = Singleton::make("copy");
echo $object->getAttributeValue(); // Will print "orginal"
Labels: Design Patterns, PHP, Singleton
Unicode Rendering in Firefox is broken.
Sunday, January 13, 2008
Its not often you get to complain about a bug in Firefox. Yes, of course there are quite a few, but in the course of general web programming you don't see them too often.But I stumbled over an absurd bug a few weeks back. (I procrastinate, hence this post is much delayed). When we tried to put some Devanagari (Hindi) text into an HTML page, it would render fine on IE 6 and 7 but come out broken on Firefox 1.5 and 2.x.
Whats the Unicode Bug in Firefox ?
If you put Unicode into an HTML page and it turns out broken in Firefox 1.5.x or Firefox 2.x , but looks fine in IE 6 or IE 7, chances are you've stumbled on to this bug.
Whats the cause?
Simple. You probably have a text-align:justify CSS style property set on the Unicode text in question. Its a weird bug, and the last place I expected to find the cause of my broken rendering issue was a simple CSS alignment property.
How to fix the Unicode Bug in Firefox?
Even simpler. Change the text-align property to any other valid value other than justify.
Is there a work around?
Nope. There isn't any. The Firefox team have been aware of this bug since Firefox 1.5, but haven't scheduled a fix till Firefox 3! Why? Well, the bug is actually resent in the core Gecko engine (so it is present in your XULRunner based Applications as well), and Firefox 2 was all about the UI fixes and clean up. So this fix got moved to Firefox 3. The good news is that Firefox 3 beta's are already here and looking good.
Is there really no work around?
Well, if you really can't change the way your site style is built, (to many users on IE to justify this change for example), or if you are not part of the sites developer team, then you can try out building a custom Grease Monkey script to fix the problem for the specific site. Be warned though, that this requires you to know JavaScript, and will require you to basically overwrite the CSS property for the site in question in your local browser.
Amit Chakradeo has tried this out.
Where can you read more?
Look up the original Firefox Bug Report:
Bug 240914 – "text-align: justify" ruins Unicode combining characters (e.g. Devanagari, Tamil, Bengali,Malayalam,Kannada)
Labels: Bengali, Devanagari, Firefox Font bug, Firefox Unicode Bug, Kannada, Malayalam, Tamil
Questions for the Campers at BarCampMumbai2
Tuesday, October 16, 2007

A couple of days ago, I was at BarCampMumbai2, and I spent some time chatting with other campers on testing PHP-Mysql based web applications.
What I did share with the campers was some of our experiences at BookEazy. More important than that however, I actually approached them with 3 issues we have not been able to solve here.
I now throw these rather well known issues to the world of developers out there in the hope that somebody will share some testing Gyan with us.
Question 1
How do you effectively test a PHP function that looks something like:
function fooBar() {
// some code here
...
...
mysql_query( [some complicated dynamically generated query string here] );
...
// Some minor error checking and result processing here
}
What I'm looking for is a solution that helps reduce the complexity of doing super huge database data setups and tear down, because we know from experience that running the big mama setup route becomes unmaintainable fast.
Question 2
How do you test the mobile version of your application/website in an automated and script-able fashion. I know of no tool that implements a Selenium/Watir/WebDriver like testing framework for Mobile platforms
Question 3
How do you test mobile application delivery and installation across multiple mobile platforms in an automated fashion.
I invite anyone with suggestions, resources to share, ideas or comments to feel free to leave your thoughts in the comments section of this post.
Labels: BarCampMumbai2, PHP, Testing
GTAC 2007, Selenium, and the UI as objects in tests.
Monday, October 01, 2007
I was going through all the clips posted on YouTube and Google Video from the recently concluded GTAC 2007. For the uninitiated GTAC is the "ask to be invited" only Google Test Automation Conference.
My pick of some the best lightning talks:
1) Code Smells
2) Selenium vs WebDriver
3) Managing thousands of test at Mozilla
Some of the 1 hour sessions posted were not really applicable to the average project team that do much smaller scale stuff than what Google attempts. Anyways, the ones I thought were pretty decent are listed below:
- Patrick Copeland - Keynote
- Simon Stewart - Web Driver for Java
- Adam Porter & Atif Memon - Skoll DCQAS
- Apple Chow & Santiago Etchebehere - Building an Automated Framework Around Selenium
Implementing the UI as objects in test code.
Now here's an idea I really like. Google's Apple Chow mentioned this idea in one of the sessions. The basic idea (at least the way I look at it) is that you represent the UI and its components as objects in your test scripts. This way you can keep implementation specifics of test for a particular page/UI component isolated from the main test script. The best pro of this approach is that whenever your UI implementation changes, and lets face it, this happens a lot in web applications, you don't have to go and fix a whole bunch of tests. You just adjust the object that modeled that UI in your test environment.
Interestingly, Simon Stewart mentioned that WebDriver creates a sort of Page object for every HTTP request. That kind of follows this methodology in my opinion. Good stuff. Great ideas, and we are going to use these :-)
"Test Training" and "Happy Path" Tests
Patrick Copeland mentioned the concept of "Happy Path" tests. Nice term for something you really want to avoid. Basic idea is that there is a very real tendency for developers to write tests that behave the way you a want a user to behave. Its a bit like testing for what a user should do instead of what a user could do. Excellent point, and good to keep in mind as a developer.
There's also the issue of "Test Training" which basically occurs when updating and modifying tests over time. Developers tend to subconsciously (or consciously??) modify tests so that they pass rather than fail. This ones hard to detect and weed out. But also a good point to remember.
Using Unit Tests for Negative Testing
Matt Heusser & Sean McMillan talked about the concept of using Unit Test to focus on negative testing since functional tests basically focus on the positive paths anyway. I like this idea a whole lot. If you also write a few unit tests to cover the positive paths, then this approach will also lend itself to the idea of using unit tests to form functional tests, and functional tests to form stress/load tests. (Frank Cohen over at PushToTest.com has been pushing that idea for a long time now.)
I'm going to make special mention of Skoll DCQAS. This talk blew me away. While not the sort thing small teams can use easily, the power of this "soon to be out there tool" is freaky. I'm not going into details, you need to watch this one. Atif mentioned something about the shortest path through an application is often the most used in the real world. I'm not entirely convinced thats true, but if it is, heck, this tools going to be worth even more!
All in all, there's a lot to be learned from these clips. What probably got me most excited was however, the fact that Google is actually actively developing Selenium. Not only are they finding and fixing a whole can of worms in Selenium, but they are trying to release a lot of back to the open source repository. If they also release their Firefox extension to Firebug, for Selenium testing, life is going to get a lot better for us Selenium followers pretty soon.
Labels: GTAC, Selenium, Testing
Installing Xdebug for PHP on Centos 4 (server)
Friday, August 24, 2007
I needed a good debugging tool for PHP 5, and decided to jump into the world of live debugging. Turns out, in the case of PHP thats not such a hot place to be in. But thats a tale for another day.After scanning the possible offerings, I narrowed it down to the two main options.
Since Xdebug was open sourced and supported a pretty nice feature list, it was promptly selected.
So then I had to go about getting installed and setup on a Centos 4 based test environment. After some searching I still hadn't found the exact instructions.
So for those of you who need to install Xdebug, here is what you need to do.
- You need to have PHP 5 and PECL installed and working with Apache already.
- You need to install PHP's devel package. This is needed for the command phpize to work. Note that you need to have the CentosPlus repository enabled for this. So from the terminal as root run the command:
yum install php-devel
- Next you need to have GCC and GCC C++ compilers installed. The only way to install Xdebug on linux as of time of writing this is to compile the extension yourself. The good thing is that this turns out to be is really easy. Note that you also need Autoconf and Automake packages. Once again from the terminal as root run the command:
yum install gcc gcc-c++ autoconf automake
- Next its time to get the Xdebug package for php itself, and compile and install it. That is as simple as typing the following command as root:
pecl install Xdebug
- Now you need to configure your PHP to load the extension. On Centos, extensions are loaded from a folder most likely to be located at /etc/php.d In this folder you are most likely a bunch of .ini files corresponding to what ever extensions you have installed for use with PHP. You now need to create a file called xdebug.ini here as root. Next add the following lines to the file:
; Enable Xdebug extension module
extension=xdebug.so
; Configure the extension [See Xdebug documentation for options to add here]
xdebug.remote_enable=
xdebug. ..... - Restart Apache with service httpd restart and check the output of phpinfo() to see the if Xdebug is loaded and configured correctly.
In case anybody was wondering where the xdebug.so lives on Centos well my system has it sitting at /usr/lib/php/modules/xdebug.so Doing a simple locate xdebug.so should help you locate it.
Anyways this should be enough to get you up and debugging. I still don't have a satisfactory visual client for Xdebug on Linux, but I shall be experimenting with Eclipse's PDT client over the next few days. Will post later on that.
Labels: PHP debugging centos linux
Learning Javascript the right way.
Monday, August 13, 2007
I think everyone's first experience with JavaScript is pretty much similar. You probably got some small code snippet from somewhere, or wrote a couple of lines of code, put it into a page, and popped open IE, ...just to see what it could do.From then on my guess is that most of you, like me, just went along writing JavaScript code, as and when fancy took us.
Can you possibly imagine doing the same thing in C ? or even Java for that matter. Of course not, because those are serious languages. Thats what I thought until I came across YUI and the amazing Douglas Crockford at the YUI theatre.
Soon after being totally brainwashed by Crockford and his lectures, I decided it was time for me to really learn JavaScript. After all programming is a serious business and for serious people right?
No sooner had I begun to look deeper into JavaScript when I found so many thing I was doing wrong when it came to this amazingly powerful and expressive language. For most part I was ignorant of JavaScript's prototypal inheritance and closures. Heck I had not even heard of JavaScript best practices.
So the week when I'm Goa, on well earned R&R, I'm going to sink myself into the world of proper JavaScript.
Labels: Javascript quality