Archive for May 2009

 
 

TwitterPHP 0.5 released and lessons learned…

I’ve released this Friday a new version of the TwitterPHP library. In this version I’ve done some code optimization, removed some files and changed to Doxygen for class documentation.

First, all methods were returning a SimpleXML object, which is a mistake in terms of flexibility. I’ve changed them all to just return a XML string. Now its up to the user (programmer) to choose the way to access/parse the XML.

Second, TwitterPHP is a class, you can instantiate it several times, for let’s say, accessing multiple accounts. At least you should be able to do that, but since I was reading the Twitter username and password from a configuration file (naive, I know), multiple instances could only use the same account. Now the problem is solved by simply adding the username and password as arguments of the constructor method and removing the configuration file.

Third and last, I switched from PHPDocumentor to Doxygen. This was mainly because I found out that Doxygen is much more flexible, I can use it with any other project with any other language. And the resulting documentation is more fast and clean than PHPDocumentor.

So you can take a look at the new version of TwitterPHP here and the documentation generated by Doxygen here.

In future releases I’m planning to add OAuth support and implement more Twitter API actions.

Object Oriented Javascript

Recently, I’ve been playing with Object Oriented Javascript. This language, besides it’s chaotic nature, can be very powerful when used with some best practices (humm, that’s funny, I’d swear I’ve heard this before).

One of the most interesting things about Javascript OO is the way to define a Class. Actually there is no keyword “class” at all. So here’s an example on how to create a class Book in Javascript:

/**this will be the constructor method,
that automatically creates the class**/

function Book (isbn, title, author) {
    this.isbn = isbn;
    this.title = title;
    this.author = author;
}

/** And now the getters **/

Book.prototype.getISBN = function () {
    return this.isbn;
}

Book.prototype.getTitle = function () {
    return this.title;
}

Book.prototype.getAuthor = function () {
    return this.author;
}

If your class has attributes that are not passed as constructor arguments, you can declare the attribute just like we did for the methods:

Book.prototype.numTimesRead = 5;

//a setter
Book.prototype.setNumTimeRead = function (n) {
    this.numTimesRead = n;
}

Now if we wish to instantiate our class, it’s identical to most OO languages:

var mybook = new Book("123", "Dive into Python", "Mark Pilgrim");

//I tried to output this the ugliest way possible :P
document.getElementsByTagName("body").innerHTML = "Title: "+mybook.getTitle();

This will (pwn your body element) print the Book title, you can add any other info as you wish.

You may find more on object oriented programming with Javascript at the Mozilla’s Developer site.