Yii 1.1: Eclipse PDT - code-hints, references and other goodies

6 followers

Intro

Since PHP is dynamically typed language, the way IDE may help you is limited.

It will hint you on methods, fields and class constants, but won't know the type of object you receive when iterating over an array.

Eclipse parses phpDoc comments to find out about function parameter and return value types, descriptions and so on.

To make best use of it be sure to add phpDocs in your code:

/**
 * You'll see this when you get a drop-down 
 * with suggestions for completing the word
 * and onHover on class name.
 */
class Foo {
 
    /**
     * Description of a field.
     * Be sure to add varType below to get you're code-hints.
     * E.g. $aFoo->bar-> + alt+space will give you drop-down
     * with varType's fields, methods and so on. 
     *
     * @var varType Description of the field
     */
    public $bar;
 
    /**
     * Description of baz
     *
     * @param $var varType [$var description]
     * @return Foo
     */
    public function baz( $var ) {
        return new Foo();
    }
}

Yii has great documentation in the source code allowing IDE to help fellow programmer a great deal.

Howto

To get code-hints you basically need to have Yii (or any other framework/library) on your build path.

There are 2 ways to achieve this:

Yii inside your project's source

Say you've got yii (framework dir from ditributed archive) at the same dir as your project's protected dir.

Then you basically don't need to do anything - once Eclipse parses the code you've got all the good stuff.

Yii outside project's source

Here you'll need to add yii (or any other framework/library) to your include path.

  1. Create project
  2. Go to Project > Properties > PHP Include Path
  3. You've got Projects tab, Libraries tab, Order (and Source in PDT 2.0).
  4. Now use your own judgement:
    • whether to create project for yii (maybe you'd like to checkout latest revisions from time to time?)
    • add Yii as a library (e.g., its in your system- or user-wide PHP includes dir)
    • add Yii as a source folder
  5. Once Eclipse parses the code you've got all the good stuff

Code Hints in Your Yii Views

Views in Yii have a minimal amount of markup but are launched in the scope of the calling controller class. The "$this" variable references the calling controller but Eclipse and other editors won't be able to provide code hints without some help from you. At the top of the view file you can put:

<?php /* @var $this SiteController */ ?>

Change "SiteController" to the name of the calling controller for each view.

Tips

  • Might happen code-hints doesn't work. Try to remove yiilite.php - it has all framework's classes inside but without documentation. Maybe Eclipse stops after parsing it.
  • Having yiilite.php will actually give you all classes twice. You might want to remove it for convenience. (Check docs on performance once you're approaching release date!)
  • Holding mouse over something in the code will give you tooltip with info on that something if any.
  • F4 will open type-hierarchy (PDT 2.0).

Total 2 comments

#78 report it
cma at 2010/09/21 04:26pm
Thanks + another tip for code-hints

It's cool to have yii as a project, you can manage vendors extension, theme, gii code templates, facilitate deployment The problem is code-hints from yiilite.php. To avoid this, simply remove it from the build-path and keep this file

-> right-click on file, Build Path | Exclude

#1683 report it
Umopepisdn at 2009/05/11 02:17am
Aptana Instructions

Those using Aptana's PHP module can achieve code completion same by going to Project, Properties, Project Natures, setting the PHP nature as the project's primary nature. Then go to Project, Properties, PHP Buildpath, and make sure Yii is referenced as an external directory.

Leave a comment

Please to leave your comment.

Write new article
  • Written by: mindeh
  • Updated by: Woil
  • Category: Tutorials
  • Yii Version: 1.1
  • Votes: +14
  • Viewed: 25,912 times
  • Created on: Feb 18, 2009
  • Last updated: Jan 31, 2011
  • Tags: IDE