by Kevin Schroeder | 12:00 am

Code Assist is one of those "must have" features in any IDE.  Some developers are so brilliant and have such good memories that they are able to know what parts of their application do what and those silly drop down boxes just get in their way.  I am not one of those developers.  I love Code Assist.  In fact, I learned Java via code completion in JBuilder 8.  It was horrible Java code, but it worked.  And eventually I learned how to write good Java… using code completion as well.

The concept of code completion is very easy to implement when you have a statically typed language.  It's not really a question of IF you will get a certain type of variable.  You WILL get it.  A dynamically typed language is a bit different.  You can have structure, but only if you decide to follow it.  I think this is a good thing.  There are many problems that don't need to have a highly structured solution.  In fact, most problems you need to solve on the web don't really need a highly structured solution.  That's one of the reasons, in my opinion, why PHP is so popular.  You can have the structure if you need it, but if you don't need it you don't need to use it.  That's also one of the reasons why I don't like rigid frameworks.

So, if you're going to give your PHP application more structure, how do you tell other developers about it?  When using Zend Studio or PDT comments are your friend.  Not just any comments, but PHPDoc.  With that you are able to both document your code and also provide hints to the IDE about what your return types are.

Let's start with a class

class User
{
    public function getParent()
    {
        if ($this->_parent) {
            return $this->_parent;
        }
        return null;
    }    
}

Now let's try some code completion on it.

Easy enough.  But what if you want to get the parent of the parent?

Parent of parent

Nothing.  And how should it?  The IDE has nothing to base the return type on and so has nothing to provide.  So how can you tell the IDE?  With PHPDoc.

class User
{
    /**
     * @return User
     */
    
    public function getParent()
    {
        if ($this->_parent) {
            return $this->_parent;
        }
        return null;
    }    
}

As you can see, we simply added @return User to our PHPDoc.  Now when we try out code completion we get

With Code Assist

Piece of cake.  But, what if we have code like this?

foreach ($users as $user) {
    $user->
}

We know that $user is an instance of User but the IDE doesn't.  However, what we can do is tell the IDE what the variable type is inline with the code.

Inline code completion

The code /* @var $user User */ provides information that the IDE can use to give you the code completion you need.  It is a simple multiline C comment with a tag @var in it.  Following the tag, the name of the variable is provided, in this case $user.  Then the variable type is provided. 

Easy.

Comments

Oliver

So why doesnt:

/* @var $GLOBALS[‘USER’] User */
$GLOBALS[‘USER’] = new User();

work?

Mar 16.2010 | 08:35 am

Kevin

Oliver, first, be careful of using global variables. They have a tendency to cause difficult-to-track errors. The reason the code doesn’t work is because the feature works with individual variables in specific scopes. Individual keys in a variable are not static and can be changed on the fly, using SPL, for example. But to get code completion on the code you entered all you would need to do is

$user = $GLOBALS[‘USER’]
/* @var $user User */

Mar 16.2010 | 08:44 am

silvasaraiva

Hello there Kevin,

I’m unable to do such a thing. Maybe is because I’ve not created a PHP project since I’m just editing “solo” files…any thoughts on that?

Best regards,

Apr 28.2014 | 05:26 pm

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.