ZendStudio for Eclipse

For professional PHP development, nothing beats ZendStudio in my book. Currently ZendStudio is in the process of moving from a standalone application to something build on
top of Eclipse. I’m sure it might be a wise move on the long term, but there are a few things bugging me with th current version. The number one issue is shown in the screenshot to the right.

Would someone please tell either Eclipse or ZendStudio, that PHP files do not need to be build, compiled or what ever it is doing – besides wasting my time for a few minutes.

Google App Engine

I’ve been playing a bit with the Google App Engine the past few nights. It’s one of the newest toys out of Google, and it could very well be a very important piece of infrastructure to many web developers trying to create a dotcom adventure.

Google App Engine (once they let you in) allows you to run web applications of google’s server infrastructure. With the Google App Engine you can write applications (and run these of your local machine (Mac, Linux or Windows) and even use data storage in your applications. Currently applications can only be written in Python, but Google promises support for other languages later. Once approved by Google and your application is ready for prime time, you simple deploy it, and it runs of Googles Servers.

You can – if you like – use your own domain and with half a gigabyte storage and 5 million page views in the free edition, it will get you pretty far.

I haven’t been approved by Google yet, so I can’t deploy my applications, but others have, and one of the first examples is Vorby: Movie Quotes. It looks like yet-another-database-powered-website, and I guess that is a major part of the point with Google App Engine – You can make most database-backed web applications with it and not needing to spend money on hosting and infrastructure until you have millions of page views and a revenue stream to support the site.

Fetching Image details in Perl

Image::Size is fine, if size is the only thing, which matters. Sometimes, however, it isn’t enough, and when that is the case Image::Info (again fetched from CPAN) is your friend. Point it to a file (through various methods), and it will return a hash with all the information available about the image you pointed at. Most popular formats are supported.

1
2
3
4
5
6
7
8
9
10
11
#!/usr/bin/perl -w
use strict;
use Image::Info;
 
# Just fetch the size
my $imgInfo = Image::Info::image_info("test.jpg");
 
# Print out all info fetched from the image
for (keys %$imgInfo) { print " $_ -> $imgInfo->{$_}n"; }
 
exit();

Converting between image formats in Perl

Changing files from one format to another is quite easy with a little bit of Magick. In the example below a JPG image (test.jpg) is converted into a GIF-image (test.gif). To output in a different (ImageMagick supported) format, just change the “image->Set” line.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/usr/bin/perl -w
use strict;
use Image::Magick;
 
my $image = Image::Magick->new();
 
# To explicitly set image format use this instead:
# my $image = Image::Magick->new(magick=>'JPEG');
 
my $x = $image->Read('test.jpg');
 
$x = $image->Set(magick => 'GIF');
$x = $image->Write('test.gif');
 
exit();

Rotating an Image with Perl

Turning images is quite simple. In the example below an image is turned 90 degrees clockwise, wirtten to a file, turned another 90 degress and written to a file again.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/usr/bin/perl -w
use strict;
use Image::Magick;
 
my $image = Image::Magick->new(magick=>'JPEG');
my $x = $image->Read('test.jpg');
 
$x = $image->Rotate(degrees=>90); # 90 degress clockwise
$x = $image->Write('test.90.jpg');
 
$x = $image->Rotate(degrees=>90); # Another 90 degress clockwise
$x = $image->Write('test.180.jpg');
 
exit();

Making thumbnails with Perl

With the help of ImageMagick you can automagically use Perl to create thumbnails. The example below is quite rude and makes a 50 by 50 thumbnail (no matter which size and shape the master had). Before using it in a real world scenario, check the aspect ratio, the size of the original image and what ever may be applicable.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/usr/bin/perl -w
use strict;
use Image::Magick;
 
my $image = Image::Magick->new(magick=>'JPEG');
my $x = $image->Read('test.jpg');
 
$x = $image->Scale(width=>'50', height=> '50');
 
# The following should also work fine...
# $x = $image->Scale(geometry=> '50x50');
 
$x = $image->Write('test.50x50.jpg');
 
exit();

CVS – Getting started

CVS is a sourcecode version control system, which is just about the best thing which can ever happen to a software project. CVS gives you the power to manage the development of your sourcecode and keep track of changes in the code.

Below there’s a few pointers to the net, where you can find lots of information about CVS and the add-on tools which supplies it. The section called “local content” is our own attempt to add something to the endless pages describing and explaing how to use CVS. Enjoy.

CVS Documentation and Information

CVS Tools and add-ons

To accompany CVS a large number of tools exist. Below is a brief list of those found most useful.

Other sources of information

PHP Coding Standards

If you’re part of a larger team developing (and sharing) code, it is a pretty good idea to have some common standards. In the PHP work, such a set of common standards include the PEAR Coding Standards. While it may be a challenge to agree on standards, it can be done – the hard part is following the standards.

One excellent tool to help you check for standards compliance is the PHP CodeSniffer. We’ve had it running for quite awhile and it seems to work as advertised. We’ve made our own (“TDC”) coding standards which is a variation over the PEAR coding standards, and checking if you follow them is as simple as running a simple command line.While it doesn’t enforce following of the coding standards, it does make it a no-brainer to check if you do follow the common standards.

The output from the codesniffer could look something like this:

mahler@mahler$ phpcs --standard=TDC Example.php

FILE: Example.php
--------------------------------------------------------------------------------
FOUND 44 ERROR(S) AND 2 WARNING(S) AFFECTING 38 LINE(S)
--------------------------------------------------------------------------------
[LINE 2] ERROR: "require_once" is a statement, not a function. No parentheses are required.
[LINE 5] ERROR: "require_once" is a statement, not a function. No parentheses are required.
[LINE 7] ERROR: Space before opening parenthesis of function call prohibited
[LINE 9] ERROR: Space before opening parenthesis of function call prohibited
[LINE 20] ERROR: Line indented incorrectly. Expected 0 tabs but found 1.
[LINE 49] ERROR: Line indented incorrectly. Expected 1 tabs but found 2.
[LINE 51] WARNING: Equals sign not aligned correctly. Expected 1 space, but found 2 spaces.
[LINE 59] ERROR: Space after opening parenthesis of function call prohibited
[LINE 61] ERROR: Line indented incorrectly. Expected 2 tabs but found 3.
[LINE 61] ERROR: Expected "while (...) {\n" found "while(...){\n".
[LINE 63] WARNING: Equals sign not aligned with surrounding assignments. Expected 3 spaces, but found 1 space.
[LINE 74] ERROR: Line indented incorrectly. Expected 1 tabs but found 2.
...

How do you know good code?

One of the great challenges of PHP is that it’s so easy to learn, that just about anyone can learn it with not too much of an effort. While this is great for the number of PHP developers, it also seems to have the effect, that there is a huge number of bad examples of code out there. How do you then know good code?
In my book there are a few signs, which you could judge from – and they may even apply broader than just php-code.

First sign: It does the job

The code should be designed for the challenge at hand. Too often developers seem to apply the same style and energy to solve a challenge – no matter how much they differ in complexity and importance. If it’s a simple little function being developed, it shouldn’t require a huge framework.

If it deals with financial or personal data, it should probably utilize transactions and apply logging. Did the developer think of the challenge they set out to solve? – if so it’s a good sign.

Second sign: Well-structed

How does the sourcecode look? Are there mile long lines of code or are they sanely formatted? Is the code broken into functions, classes or another structure – or is it just page after page of sourcecode? – I don’t need to see all code as classes nor as neat function-libraries, but I do like if the developer has made an effort to break an application into some manageable pieces somehow.

Third sign: Reasonable naming scheme and comments

How does the function names, classnames and variable names look? is it random garbage or does it make sense? – I really hate variables named from $a to $z – and I do hate functions named “doSomething” – without specifying it further. I would expect great code to utilize the same naming conventions (CamelCaseing, underscores and so on) across all functions and/or variables.

If strange – as in unnatural/unexpected – things happen in the source code, I would expect a (short) comment explaining what’s going on.

Fourth sign: Security and Contingency

Did the developer think about security? is the code wide open for xss attacks? is input validated? Is “the unexpected” handled gracefully or does the code explode if exposed to simple URL-manipulation? Do you know what a SQL-injection is? If you need data from another source, what happens if it isn’t available? – does the code blow up or does it burn gracefully?

How do you recognize good code?