Developing software in the Real World

Using PDT with Zend Framework Projects

I original wrote this as a comment on Victor Nicollet’s blog, but I thought I should document it here too so that I can refer other people to the information.

These are some tips and tricks when using PDT with Zend Framework that make my life easier:

Autocompletion for dynamic properties

Zend_Db_Table_Row_Abstract uses __get() and __set() magic in order to map to the underlying database table row in question. This means that you can use property autocompletion on an instance of it. You can however tell your your table class which class to use for the row objects:
class Application_Model_Users extends Zend_Db_Table_Abstract
{
protected $_name = 'users'; // database table name
protected $_rowClass = 'Application_Model_User'; // row class to use
}

With your row class, you can take advantage of the @property docblock element to document the fields in the class:

/**
* @property string $user_id
* @property string $username
* @property string $password
*/
class Application_Model_User extends Zend_Db_Table_Row_Abstact
{
}

Autocomplete on instances of Application_Model_User will now work.

Zend_View scripts are more complicated as they use $this to access magic properties. You can do however use the @var trick at the top of the .phtml file:

$user = $this->user; /* @var $user Application_Model_User */
$page = $this->page; /* @var $user Application_Model_Page */
etc...

You can now autocomplete on $user and $page. This also has the side-effect of documenting which view properties are used in this view script.

Opening an arbitrary method

View helpers are also magic methods on Zend_View’s $this within a script file. This means that you can’t cmd+click( or press F3) on the view helper’s method name to jump directly to the code for that method. You can however take advantage of PDT’s Navigation->Open Method feature. Simply select the method name and then press shift+cmd+m. The “Open Method” dialog appears and usually the view helper is selected as the first item. Just press return to go directly to the code for that view helper.

Opening an arbitrary file

For opening up an arbitrary file that’s in my project, I find that PDT’s Navigation->Open Resource (shift+cmd+r) is handy. For TextMate users, this is similar to the cmd+t feature.

Opening an arbitrary class

Using Navigation->Open Type (shift+cmd+t), you can also jump to classes easily. e.g. I regularly hit shift+cmd+t followed by “*Model_” and I get a list of all my models and can jump to the one I want using the down arrow key and then return.

Finding the current open file in the project tree

Just press cmd+option+w. You have to be careful here as it’s very similar keyboard shortcut to close and close all! However if you have an entity file open and you name your files like I do, then the mapper is next to it in the project list, so it’s only a couple of keystrokes to open it up.

Conclusion

These tips obviously work with all frameworks and any PHP coding :) Netbeans also has similar features, so I would guess all IDEs do. If you are wedded to vim or such like, then ctags is helpful for jumping around your code.

If you have a favourite tip that I haven’t mentioned, let me know in the comments!

11 thoughts on “Using PDT with Zend Framework Projects

  1. I think there may be an error in your example of declaring dynamic class properties. Shouldn't it be docblock notation (e.g. @property string $user_id)?

  2. Adrian,

    'cmd' is a key on my Mac's keyboard. I don't know what the equivalent shortcuts are on Windows or Linux though.

    Regards,

    Rob…

  3. For Windows and Linux just replace the "cmd" key by the "ctrl" key

    Nice tips btw!

    Also, shift+ctrl+l gives you a list of shortcuts (on linux and windows :) )

  4. Navigating between PDT views:
    alt+shif+Q and select your view, for instance:
    alt+shift+Q, E – opens PDT file explorer.

    While using editor:
    Ctrl + O – shows quick outline,

    when editor's cursor is on the class/variable name
    F3 – navigates to its definition
    F2 – shows documentation of the selected class

    Occurences:
    if you put the editor's cursor on the variable all its occurences in the file will be marked – so its nice way to search for variables spelling mistakes ;)

  5. If you are using Eclipse PDT, we have an Eclipse plugin that might make your life even easier. nWire for PHP is an innovative Eclipse plugin (works with Eclipse PDT & Zend Studio 7) which accelerates PHP development by helping developers navigate through their code and better understand the architecture of their application. nWire provides real-time PHP code analysis and includes the following unique tools:

    * Code visualization – interactive graphical representation of components and associations.
    * Code navigation – unique navigation view shows all the associations and works with you while you write or read code.
    * Quick search – search as you type for methods, fields, file, etc.

    We have a short demo video on our web site. We recently released an updated version which is already getting good feedback from the community.

  6. Thanks for this help! I couldn't bring to work Autocompletion with dynamic properties, but I was doing a mistake. I was making such annotation:

    @property $id

    And to make it working Eclipse needs explicite property type:

    @property int $id

    What clearly sees in example, but somehow I didn't see it.

Thoughts? Leave a reply

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