Email Updates RSS Subscribe
Line

This blog is created and maintained by the technical team at Hook in an effort to preserve and share the insights and experience gained during the research and testing phases of our development process. Often, much of this information is lost or hidden once a project is completed. These articles aim to revisit, expand and/or review the concepts that seem worth exploring further. The site also serves as a platform for releasing tools developed internally to help streamline ad development.

Launch
Line

Hook is a digital production company that develops interactive content for industry leading agencies and their brands. For more information visit www.byhook.com.

Line

PHP Type Hinting with Eclipse

Line
Posted on August 11th, 2010 by Norm McGarry
Line

Are you a flash developer that occasionally has to dabble in php and hates the lack of strict typing and misses the ease of type-hinting like there is with actionscript? Well, any developer that is comfortable with Flex Builder will welcome Eclipse PHP Development Tools. Flex Builder, which is built on the Eclipse framework, functions almost identical to Eclipse with PDT. I’ve used Dreamweaver, Coda, Notepad++, jEdit, Komodo, PHP Designer — I’ve used almost all of them — to write PHP, and nothing is as powerful as Eclipse (in my opinion)! Plus, it is free! So, this is a post I’ve been meaning to write for a while and it should be short and sweet. There are a few features that took me a while to find that help greatly with writing PHP in Eclipse, so I thought I would document them here for those of you who wish they would have learned about these features years ago like I do.

Type Hinting

I’m an object-oriented coder that loves type-hinting. Type-hinting, if you aren’t familiar, is when the IDE automatically suggests variables and autocompletes your coding for you. In object-oriented coding, this is an amazing feature when accessing objects that contain many methods and variables.

Ctrl+Space (or Apple+Space)
Ctrl+Space is such an amazing key stroke. I potentially hit that key combination 1-3 times per line of code. Ctrl+Space will give you a drop-down window that prompts of suggestions to finish the variables or method you are typing out. It lists variable names, function/method names, data types to instantiate a new variable, as well as a list of all the built-in PHP functions. Simple, yet so beautiful.

Code Commenting

In a strict language like AS3, the IDE can just pick up on the type of a variable and handle code-hinting automatically. However, since PHP is a dynamically typed language, this isn’t the case. For example, methods and functions do not have a defined return type and it can be impossible for an IDE to know how to type a variable that gets it’s value from a method/function.

For example:

function someFunction($param)
{
 
	return "hello world";
}
 
$val = someFunction();

An IDE will have no idea how to type this value. However, with Eclipse, they have built in this amazing feature to handle that for you: code commenting. With a special syntax available in comments, you can define parameters with @param type $var_name, returns with @return type, and inline variables with @var $var_name type. This syntax based on PHPDocs and ASDocs and JDocs. For documentation on what is available, which seems to be right inline with Eclipse’s auto-complete features in every language, see this document: phpDocumentor tags.

/**
* My function description.
* 
* @param string $param
* @return string
*/
function someFunction($param)
{
 
	return $param . " world";
}
 
$val = someFunction("hello");

Now, when the IDE automatically will recognize that $val is a string. Obviously, not much type-hinting that can occur on a string, so let’s show a more complicated example.

 
/**
 * User
 */
class User
{
	/**
	 * Id for the user in the database
	 *
	 * @var int
	 */
	public $user_id;
	/**
	 * Name of the user.
	 *
	 * @var string
	 */
	public $user_name;
	/**
	 * email of the user
	 *
	 * @var string
	 */
	public $user_email;
	/**
	 * Role of the user
	 *
	 * @var Role
	 */
	public $role;
}
 
class Role
{
	/**
	 * Id of the role in the database.
	 *
	 * @var int
	 */
	public $role_id;
	/**
	 * Name of the role. (example is an administrator)
	 *
	 * @var string
	 */
	public $role_name = "Administrator";
}
 
class BlogEntry
{
	/**
	 * Author of the blog entry.
	 *
	 * @var User
	 */
	public $author;
	/**
	 * Publish date and time.
	 *
	 * @var DateTime
	 */
	public $publish_date_time;
	/**
	 * Title of the blog entry.
	 *
	 * @var string
	 */
	public $title;
	/**
	 * Body of the blog entry.
	 *
	 * @var string
	 */
	public $body;
	/**
	 * Populates the variables.
	 *
	 * @param string $title
	 * @param string $body
	 * @param User $author
	 * @param DateTime $date_time
	 */
	public function populate($title, $body, $author, $date_time)
	{
		$this->title = $title;
		$this->body = $body;
		$this->author = $author;
		$this->publish_date_time;
	}
}

Now, the IDE will automatically type the following code with ease:

$user = new User();
$user->user_id = 1;
$user->user_name = "Norm";
$blogEntry = new BlogEntry();
$blogEntry->populate('sample title', 'sample body', $user, new DateTime());
echo($blogEntry->date_time->format("Y-m-d H:i:s"));
echo($blogEntry->author->user_name);

The IDE will automatically display a drop-down milliseconds after you type “->” on the objects with a suggestion of variables and methods in the DateTime and User objects. This feature is amazing when you are dealing with hundreds of objects. It highly reduces the time spent looking through documentation to figure out variable names in an object.

Inline Typing

Most of that is pretty obvious and is pretty easy to manage, but there were certain scenarios where I did not know how to get the type hinting to work. For example, a loop!

$me = new User('Norm','norm@byhook.com',new Role("Administrator"));
$client = new User("John Doe","hello@byhook.com",new Role("client"));
$users = array($me, $client);
 
foreach($users as $user)
{
	echo($user->user_name . " - " . $user->role->role_name . "\n");
}

In this situation, the IDE wont know how to type the $user variable in the loop, because it does not know the type of the contents in the $users array. Thankfully, we can type variables inline inside the code. We simply add the following to our loop:

/* @var $user User */
foreach($users as $user)
{
	echo($user->user_name . " - " . $user->role->role_name . "\n");
}

Now, the IDE will automatically recognize the $user variable as being of the type User.

It is possible to have one function/method return multiple types and that CAN be a very convenient feature, but again, I like to be strict in my typing and I love code-hinting too much to do this. I personally don’t like to switch from a string to an integer to an object on one variable, even though PHP allows it. I prefer code that is easy to understand and highly reusable. However, it is just a matter of opinion! Hope it helped!

Line
7 Responses to “PHP Type Hinting with Eclipse”
  1. Sandesh Magdum says:

    Will this code work if I execute this script?

    Because php does not type cast $publish_date_time variable automatically to DateTime.

    This code will be useful in code completion feature only and won’t execute practically.

    Note: There is typo in above code. The variable name should be $publish_date_time and not $date_time.

  2. Tim says:

    Hi

    sorry for the quick comment, just find a solution.
    I need to “add PHP support”. (my project is not a native PHP project)

    thanx

  3. Tim says:

    Hi,

    Thanks for the post.
    But It doesn’t work for me now. I’m using the latest Eclipse Helios version with PDT 2.2.

    is there any additional setting for this?

  4. good post php and Dreamweaver

  5. Hi, I’m learning new Dreamweaver would be very helpful for me thanks

  6. [...] This post was mentioned on Twitter by tony murphy, Norm McGarry. Norm McGarry said: New blog entry – PHP Type Hinting with Eclipse – Check it out! http://bit.ly/aliBJi [...]


Leave a Reply

*

Line
Line
Pony