Tuesday, March 22, 2011

PHP Getter/Setter method generator

Here's a utility I created that generates getter and setter methods for a PHP class.  Copy and paste your PHP class into the textbox, then click generate.  It uses a regular expression to identify the private and protected fields and generates a getter/setter pair for each.  You can also customize the indentation and newline preference of the generated code:

Wednesday, March 16, 2011

Running PHPUnit from Eclipse

If you've ever written JUnit tests in Java with Eclipse, you'll know that JUnit is integrated right into the IDE.  Eclipse doesn't give you built-in unit testing support with PHP, but it's still possible to run your tests without having to leave the IDE.

In this screen cast, I'll show you how to run PHPUnit from Eclipse.

Monday, March 14, 2011

PHP PEAR libraries and Eclipse

If you develop PHP applications using Eclipse, here is a tip that may come in handy.  PDT, the Eclipse plugin for PHP development, will, among other things, display the documentation for any method, function, or field that you hover your mouse over.  And you can jump to the definition of that method, function, or field by control-clicking on it.

But if your application uses libraries installed with PEAR, then this extra functionality will not work for those libraries.  However, this can be fixed by providing Eclipse with the locations of these libraries.

Note: Click on the screenshots below to see a larger image.

1. Right click on your PHP project and select "Properties".

2. Select "PHP Include Path" from the left-hand menu.  Click the "Libraries" tab, then click "Add Library...".

3. Select "User Library" and click "Next".

4. Click "Configure...".  Another new dialog will appear.

5. You want to add a new library, so click "New..." and type in a name for the library.  I'm going to add PHPUnit.

6. Select the newly added Library and click "Add External folder...".  This is where we point to the file system location of the library.

7. Select the path to the library.  In Windows, PEAR installs its libraries inside the "PEAR" folder under the PHP installation directory.  On Mac OSX, its location is "/usr/local/PEAR".  On Ubuntu, it should be at "/usr/share/php/PEAR".  If you can't figure out where your PEAR directory is, you can find it in the "include_path" setting in your php.ini file (when PEAR is installed, it automatically modifies the PHP include path to include itself).  Click "OK".


8. Click "OK", then click "Finish"!



In my next post, I'll explain how to run PHPUnit unit tests from Eclipse without needing any plugins!

Do you have any tips for developing in PHP on Eclipse?  Let me know in the comments section!

Sunday, March 13, 2011

Freeing hard disk space in Ubuntu Linux

Here are some helpful disk cleanup tips that I came across during my quest to free up space on my tiny 4GB netbook hard drive.

1. apt-get

This command is what you use in Ubuntu to install and update the applications on your computer.  But apt-get can also be used to free up disk space:

sudo apt-get clean
Deletes package files that were downloaded when installing or updating software.

sudo apt-get autoclean
Like "clean", but only removes packages which are no longer in the Ubuntu repository or for which a newer version exists.

sudo apt-get autoremove
Removes dependencies that are no longer used by any applications.  For example, a spell checker library may have been installed along with OpenOffice.  However, when OpenOffice is uninstalled, the spell checker might not be.

2. du

This command won't itself free any space, but it can show you which files and directories are the biggest hogs (maybe there's a large video file buried somewhere that you don't know about).  The following command will recursively list all files and directories in the given directory and sort the list by file size.

du -a folder/path | sort -n


It's going to list every single file, so the list might get quite long.  It might be good to direct the output to a file and reverse the sort so the largest files appear at the top of the file:

du -a folder/path | sort -nr > disk-usage.txt

3. localepurge

If you can only speak one language like me, then check out localepurge.  It will remove all help files from your system that are in languages you don't understand.  It automatically runs every time you install any new software.

sudo apt-get install localepurge

As soon as it is installed, it will ask you for the languages you want to keep.  For example, I selected "en", which is the two letter code for "all variants of the English language" (American English, British English, etc).  I selected this instead of just "en_US" (American English) to err on the side of caution.  Note: Press the spacebar to select a language from the list and then press enter when you are done.



Then, run the program:

sudo localepurge

It will go through your hard drive and delete various files such as man pages.  It freed a good 53MB for me:



4. Language packs

You can also try removing language-related packages using the Synaptic Package Manager.  Search for "language" and remove all languages that you have no need for.  Sort by the left-most column so that the installed packages appear first in the list.  This freed even more space for me--about 200MB.


Do you have any tips for freeing up hard drive space?  Let me know!

Saturday, March 12, 2011

Setting up a Virtual Host in Apache 2 on Linux (Ubuntu)

When you install Apache on your Linux computer, it is configured to treat all files in "/var/www" as the web root by default. To run a website locally on your machine, you could copy all files into this directory. However, this is not the best way to go if you are doing serious development work, such as designing multiple websites on the same computer.

What you should do is create a virtual host. This allows you to store your website files anywhere you want:

1. Configure Apache
First, you have to add a virtual host to Apache. At least on my distro (Ubuntu), Apache splits its configuration settings into multiple files for organizational purposes. The "apache2.conf" file is the "root" file and imports all the other files. Virtual hosts are defined in the "/etc/conf/apache2/sites-enabled" directory, each inside their own file. The name of the file does not matter. Create a new file in this directory and enter the following text, replacing paths where necessary:

<Directory "/home/michael">
 Order Deny,Allow
 Allow from all
</Directory>

NameVirtualHost  127.0.0.1

<VirtualHost 127.0.0.1>
 DocumentRoot "/home/michael/testhost"
 ServerName testhost.local
</VirtualHost>

The "ServerName" will be the URL you use to access your website. ".local" is a naming convention used in the industry for running websites locally.

Apache must always be restarted whenever you change its conf settings:
sudo service apache2 restart
Tip: In order to edit these files, you have to open them as root. Using a command-line text editor can be cumbersome, especially if you need to have multiple files open at once. Instead, just open up a GUI text editor as the root user. For example:
sudo gedit &
2. Edit hosts file
Next, edit your host file. On Linux, this is located at "/etc/hosts". Add the following line at the end, replacing "testhost.local" with the "ServerName" parameter in the Apache conf file.:

127.0.0.1 testhost.local

This will tell your browser to access this URL from your local machine, instead of trying to find it out on the Internet.
Tip #2: In Linux, it can be hard to remember the locations of all these configuration files, especially because they can change depending on the distro. Any time you come across a new config file you haven't seen before, store its location in a centralized place, such as in a text file in your home folder. That way, you'll know exactly where to find it the next time you need it.
3. File permissions
Make sure the folder and all files in it are globally readable. Otherwise, Apache will not be able to see them and you will get "Forbidden" errors.

chmod -R 744 /home/michael/testhost

Tuesday, December 14, 2010

Google's Cr-48 netbook

If you haven't heard, Google is releasing its own operating system, called Chrome OS (check out this marketing video). It's based on Linux, but is tailored solely for use with online, web-based applications, such as Gmail and Google Docs. They are also releasing a netbook, called Cr-48, that comes installed with Chrome OS. The whole idea is that all your data is stored in the (mysterious, but reliable) cloud, so if something bad happens to your computer, none of your data is lost. It's almost like we're returning to the dummy-terminal days of yore. A pre-production version of the netbook has been given to a number of people so that Google can get feedback on the device and improve upon it before releasing it to the public.

One thing that differentiates Cr-48 from other netbooks is the keyboard. Google modified it slightly to better integrate it with the OS. The "Caps Lock" key was replaced with a "Search" key, which opens a new tab in the browser. The function keys (F1, F2, etc) were replaced with keys that perform tasks such as controling the brightness of the screen, controlling the speaker volume, and going back and forward in the browser.

The touchpad works as follows: Tap it with one finger for a left mouse click, tap it with two fingers for a right mouse click, drag two fingers across its surface to scroll. People online have expressed frustration with this setup, but this is exactly how the touchpad of my Asus Eee netbook functions and I'm satisfied with it. Cr-48 comes with a single USB port with extremely limited functionality. It doesn't support anything other than a mouse or keyboard, not even a thumb drive. It comes with an SD card slot, although Chrome OS currently doesn't recognize it. A headphone jack is included and is fully functional. It has a 16GB solid state hard drive.

In terms of internet connectivity, Wi-Fi and 3G (which is on Verizon's network) are supported. You get a free 100MB per month of 3G bandwidth for the first two years. The netbook doesn't have an Ethernet jack, so you're limited to wireless connections. It comes with a VGA port, allowing you to plug in a larger monitor or a projector.

The idea of an OS which stores all of its applications and data in the cloud is an interesting concept, but I'm in no hurry to start using it. Even though Google provides very well designed online applications, I would imagine that they are not as powerful as their desktop equivalents. Cr-48 sounds more like something you'd take with you on the road or to the coffee shop to do casual work.

Friday, December 3, 2010

Java: The Next Generation

A few weeks ago, plans for the next version of Java were released. It will be split into two different versions in order to more quickly get a new version out the door. Two key features that were originally slated to be released in Java 7, will be pushed back to Java 8.

One of these features is called Lambda, which adds closures to Java. From what I understand, a closure is like a function that can be assigned to a variable. This means that the closure can be passed as an argument to another function, and then called inside of the function's body. If the closure is declared inside of another function, it has access to the parent function's local variables. Many languages support this. Here is an example of how closures can be used in Javascript (taken from Wikipedia):

// Return a list of all books with at least 'threshold' copies sold.
function bestSellingBooks(threshold) {
  return bookList.filter(
      function (book) { return book.sales >= threshold; }
  );
}

The function filters the bookList array, returning only the books that have sold a mininum number of copies. It does this by passing a closure into the filter() method, which gets executed on every book in the array. If the closure returns true for a particular book, then that book is filtered out.

The other major feature that was pushed back to Java 8 is called Jigsaw. The goal of Jigsaw is to break the JDK into separate modules. This would help applications boot up more quickly. It also would help in situations where a JRE is packaged with the application. This JRE could be reduced in size by only including the modules that the application needs.

Java 7 is slated for release in mid-2011 and Java 8 is to be released sometime in 2012.