Google is translating my code!

Recently I added the ‘Google Translate’ widget to my sidebar. After some testing I noticed something problematic: All of the code snippets in my posts were also being translated. Unfortunately the PHP interpreter does not speak Spanish (it’s not that kind of interpreter apparently). Fortunately there is a simple solution. The content of any HTML element with the class ‘notranslate’ will not be translated; all you have to do is add the class ‘notranslate’ to the element containing your code.

If you’re using a plugin to format and display your code this may not be so simple. Currently I’m using SyntaxHighlighter Evolved which has a convenient field in the settings page for adding addition classes to the ‘pre’ element used to display code. If this isn’t the case for you you’ll have to do some digging in the plugin code. Alternatively you could use javascript to add the class to a particular element. This way you won’t have to make any modifications to core plugin files, which will be overwritten if you decide to update.

Here is a 100% untested example (using jQuery):

$("pre").addClass("notranslate");

The above code should prevent any content within a ‘pre’ element from being translated by Google.

NoSQL

Recently I’ve been working with a non-relational, graph DBMS called Neo4j. I’ve really only scratched the surface and it might just be a the temporary euphoria of working with something so new, but it feels liberating to be able to approach problems in a new way. It might also be that for the first time in a long time I’m working in Java. At this point I’ve written far more PHP code than Java, but I still feel like Java is my native language.

I think anyone who works with database driven applications should at least try some form of non-relational database, if only just to see things from another perspective.

Is SHA1 Still Viable?

Lately whenever I see discussion regarding SHA1 in the context of password hashing or user management it usually involves someone claiming that SHA1 has been ‘cracked’ or is otherwise not viable as a hashing algorithm. I think there is some degree of truth to these claims. In spite of this however, I think that many of these conclusions are based on a misinterpretation of the evidence.

Before I explain myself I want to say this: If you are reading this article because you intend to implement something that require a secure hashing algorithm stop thinking about SHA1. There are a lot of more collision resistant algorithms for you to chose from. You might be thinking that this fact defeats the purpose of discussing the viability of SHA1, but considering all of the existing system that rely on SHA1 I think the discussion is valid. Here we go:
Continue reading

Fetching Remote Content in PHP

Reading in remote content with PHP can be an incredibly simple task:

$url = 'http://example.com/foo.php?bar=1';
$remote_content = file_get_contents($url);

One problem with the above solution is that it requires allow_url_fopen to be enabled in your php.ini. If you’re writing a portable application that depends on being able to fetch remote content you probably don’t want to tell your users to modify their php.ini. Doing so would mean turning away users who don’t have access to their php.ini (as is the case with some shared hosts). Ideally you would want a solution that offers some redundancy.
Continue reading

Doing Things the Hard Way

Every now and then I discover something that makes me feel like I’ve been doing things the hard way. The feeling is an interesting combination of excitement and embarrassment. I experienced this today while browsing PHP documentation. I discovered two function that will make my life a lot easier in the future: debug_backtrace and debug_print_backtrace. As someone who has written his fair share of Java, I’m very accustomed to stack traces. The fact I’ve made it as far as I have with PHP without them leaves me feeling amazed. The fact that I’ve had access to them all along leaves me feeling ashamed.

I guess you learn something new everyday.

PHP Overloading

The lack of function overloading is one of my biggest complaints about PHP. If you’re not familiar with overloading, its a feature of some languages that allows you to defined multiple methods (or functions) with the same name, but taking different paramenters. In Java, for instance, methods are not identified solely by their name. Instead, functions are distinguished by their signature: their name, return type, number of parameters and their types. This allows you to do things like define multiple constructors for a class.

PHP not being a typed language, using parameter types and return types as part of a method or function signature is out of the question. You could, however, use the number of parameters. In my opinion, you should be able to do something like this:

class Foo {
	function __construct()
	{
		//constructor that takes no parameters
	}
	function __construct($param1)
	{
		//constructor that takes 1 parameter
	}
	function __construct($param1, $param2)
	{
		//constructor that takes 2 parameters
	}
}

If you did this, however, you would get an error message reading something like this: Fatal error: Cannot redeclare Foo::__construct. As unfortunate as this is, it is possible to devise a simple workaround, due to the fact that a method can be written to take an arbitrary number of parameters. The solution is to create a method that can take any number of parameters and have that method delegate to ‘helper’ methods:

class Foo {
	public function __construct()
	{
		$args = func_get_args(); //any function that calls this method can take an arbitrary number of parameters
		switch(func_num_args())
		{
			//delegate to helper methods
		case 0:
			$this->construct0();
		break;
		case 1:
			$this->construct1($args[0]);
		break;
		case 2:
			$this->construct2($args[0], $args[1]);
		break;
		default:
			trigger_error('Incorrect number of arguments for Foo::__construct', E_USER_WARNING);
		}
	}
	private function construct0()
	{
		//constructor that takes no parameters
	}
	private function construct1($param1)
	{
		//constructor that takes 1 parameter
	}
	private function construct2($param1, $param2)
	{
		//constructor that takes 2 parameters
	}
}

It might not be pretty, but using this method you will be able to call the constructor of Foo as if it were actually overloaded:

$x = new Foo();
$y = new Foo(1);
$z = new Foo(1, 2);

An alternative to this would be to use default parameters to determine what action to perform, but this method gets increasingly clumsy as the number of arguments grows.

Resurrecting a Laptop for Development

I have an old single core laptop that doesn’t get a whole lot of use anymore, partially because it is dated (purchased in 2006) and partially I’ve never really adapted to using a laptop as a general purpose computer. Increasingly, however, I find myself in need of a laptop either when I want to get some work done when I’m away from home, or when I need to take my work with me. This being the case I decided to re-purpose my old laptop as a dedicated development machine.

I could have just put a fresh copy of Windows XP on it and gone from there, but given the circumstances that might not have been the best route. All I needed to run was apache, MySQL, PHP, subversion, and something to write code in. Clearly something more lightweight than Windows could accomplish this. In addition to this I wanted something that would mirror my production server, which is running ubuntu. I ended up installing lubuntu, a lightweight distribution based on ubuntu (if you plan on trying lubuntu bear in mind that it is still in beta). Other than some very minor bugs, lubuntu seems to be perfect for my needs: I could configure it to be almost identical to my production server, it is easy on resources (even on my ancient laptop), and I immediately noticed a big improvement with regard to battery life.

Hello World Nightmare

Traditionally, when someone is introduced to programming, the first block of code they write is the infamous “Hello World” program. Unless you’re dealing with some esoteric language this program usually consists of just a few or even a single line. There is one popular language out there where, in comparison, Hello World is a monster: Java. If you’re a Java coder you may not see it, especially if Java wasn’t your first language. Try to look at it from the perspective of a first timer:

public class HelloWorld {
	public static void main(String[] args)
	{
		System.out.println("Hello World");
	}
}

In addition to a huge dose of OOP some of the concepts bundled into this small block of code are: visibility and scope, arrays, return types, and data types. How do you explain static to someone who has no concept of an object? I explicitly recall, when taking my first Java course, my professor referring to static as “Voodoo Magic”. Compare this to something like C++:

int main()
{
	cout << "Hello World";
	return 0;
}

Granted the whole << thing might seem a bit strange, but overall this is much easier to explain. Don't even get me started on PHP:

echo ‘Hello World’;

Don't get me wrong. I think Java is a great language for beginners. It's balance between ease of use and structure keeps new comers from punching their monitors while at the same time adhering to a fairly strict set of rules.

RosettaCodex

As someone who is already experienced with a few programming languages, learning new languages, frameworks, and platforms is a much different process from someone who has no background in programming. Obviously having written a ton of code make learning how to write different code much easier. Much of the documentation out there however does not seem to take advantage of this fact. Documentation for someone who is completely new to programming is the same as documentation for someone with years of experience. It might be easier for the experienced programmer to understand and utilize that documentation, but it is still the same documentation.

The problem here, for an experienced programmer, is that all of this documentation exists in a bubble. It does not assume any prior experience and it does not use other languages and frameworks for reference. Clearly the reason for this is because any reference to alternative approaches would be wasted on an unexperienced coder, and might even be confusing or distracting.

My solution is to create an encyclopedia of problems with solutions in as many languages, frameworks, and platforms as possible. By now, if you’ve read the title, you know I’m calling this solution RosettaCodex. The concept is the same as the Rosetta Stone. If the same thing is written in multiple languages and you know at least one, you can figure out the meaning of the others.

RosettaCodex is a cross-language reference created specifically for those with programming experience. If you think this is a good idea feel free to contribute.

Check it out: RosettaCodex