Sofware Development Archive

Pingback-php: a library for performing Pingback calls in an easy-way

Today I’m pleased to announce another library that I made. Pingback-php is a library for performing Pingback requests in a simple way. Pingback-PHP is compliant with the Pingback 1.0 standard specification.

A quick example:

If you want to inform to this article that you have referenced it from one of your posts by using the Pingback protocol, you can do it with this code:

// Prepare the Pingback client
$requestHandler = new Pingback\RequestHandler();
$client = new Pingback\Client($requestHandler);

// Perform the pingback call
try {

    $client->ping(
        "http://www.mabishu.com/blog/2012/12/14/get-better-performance-and-life-from-your-ssd-in-linux-based-systems/",
        "http://www.mabishu.com/blog/2012/12/14/object-calisthenics-write-better-object-oriented-code/";
    );

} catch (Pingback\Exception $e) {
  printf("Exception raised with code (%d) : %s\n", $e->getCode(), $e->getMessage());
}

Object Calisthenics: write better object-oriented code

“Object Calisthenics” is supposedly an exercise to get you to write better object-oriented code. If you want me to sum-up in one sentence I will definitely say:

“That which obscures my code is bad.”

We’ve all seen poorly written code that’s hard to understand, test, and maintain. Object-oriented programming promised to save us from the old procedural code. And it promised allowing us to write reusable software incrementally. But sometimes it seems like we’re just chasing down the same old complex, coupled designs in any OO-capable language (Java, PHP, …) that we had in C.

It’s well understood what are the core concepts behind good design, any software engineering book will hightlight seven code qualities that matter: cohesion, loose coupling, no redundancy, encapsulation, testability, readability, and focus. Yet it’s hard to put those concepts into practice there are some other rules that could help us.

Test your code with every change in your PHP files

Applying TDD to your PHP code is a big improve in your code as forces you to “think” the code before write it down. A common approach to TDD workflow could be:

  1. Write your test class
  2. Write your code that make pass the tests
  3. Refactor the code

If you take a closer look, between all the steps you MUST run all the tests for possible functionality breaks with every single change in the code, and make it manually could be a little disturbing.

So for this there are  tools that helps you track your code for changes and if changed will automatically run a bunch of actions.

Sending patches through mail with ‘git send-mail’

If you want to send code patches to your coworkers you don’t need to do strange things with different tools. Git has all the neccesary tools for handling this in a pretty way.

The idea is generate a patch from git and send to your coworker via email. After received the message they can import those messages directly into git.

Let’s see how all this works.

Sort an array of objects by one of the objects property with PHP

Quicksort algorithm animationIf you want to sort an array of objects but using one of its object properties you can do it with usort, but this could take a lot of time and will be very computational expensive.
So I have implemented one method you can insert into one of your PHP classes. This method implements the quicksort algorithm.

As a result I have experienced a huge performance improvement against usort: one array of objects with 10.000 elements sorted with usort takes ~3.4 sg, and with quicksort algorithm takes, in my samples, ~0.56 sg. Awesome isn’t it?

Page 2 of 3