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


 DocumentRoot "/home/michael/testhost"
 ServerName testhost.local

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.: 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