2

I know this question has been asked numerous times but in my case, it's just for the sake of convenience when writing codes in Netbeans. I've been working with PHPExcel library and because it has too many methods, I can't possibly remember all of them. So I need the auto-complete feature of netbeans. Problem is, netbeans only autocomplete up to a certain extend.

For example :-

protected function _read_excel(PHPExcel $excel)
{
    $sheet = $excel->getSheet(0);   // Works perfectly fine here because I type cast the argument
    $rows = $sheet->getRowIterator(); // Works fine here too
    foreach($rows as $row) 
    {
        $cols = $row->getCellIterator(); // By the time I reach here, Netbeans stops auto-completing. Probably due to Netbeans don't know what class it is.
    }
}

If only I can do this :-

$cols = (PHPExcel_Worksheet_CellIterator) $row->getCellIterator();

I know PHP can't do that but is there a workaround for this?

2 Answers 2

4

You need to start commenting your code using PHPDoc type comments, Netbeans will use them for auto completion.

protected function _read_excel(PHPExcel $excel)
{
    $sheet = $excel->getSheet(0);   // Works perfectly fine here because I type cast the argument
    $rows = $sheet->getRowIterator(); // Works fine here too
    foreach($rows as $row) 
    {
        /**
         * @var PHPExcel_Worksheet_CellIterator $cols
         */
        $cols = $row->getCellIterator(); // Now netbeans will know what class it is.
    }
}

If you prefer, you can use 'inline' docblocks too :-

/** @var PHPExcel_Worksheet_CellIterator $cols */

Or, in the latest version of NetBeans, the order appears reversed and has one fewer *

/* @var $cold PHPExcel_Worksheet_CellIterator */

However you must make sure that netbeans knows where to find PHPExcel. My answer here gives some guidance on that if you need it.

Sign up to request clarification or add additional context in comments.

1 Comment

Hey.... This is really helpful. Thanks. I also found that vdoc followed by the TAB key is the shortcut to declaring variable.
0

Thanks to @vascowhite for enlightening me.

So for the sake of others, to declare a variable in comments (in Netbeans) just type vdoc followed by TAB key.

Based on that, the format is actually @var $variablename ClassName. Also, for a foreach loop, you have to declare the variable outside of the loop.

Example:

protected function _read_excel(PHPExcel $excel)
{
    $sheet = $excel->getSheet(0);   // Works perfectly fine here because I type cast the argument
    $rows = $sheet->getRowIterator(); // Works fine here too
    /* @var $row PHPExcel_Worksheet_Row */
    foreach ( $rows as $row )
    {
        /* @var $cols PHPExcel_Worksheet_CellIterator */
        $cols = $row->getCellIterator(); // Now netbeans will know what class it is.
    }
}

4 Comments

The problem with doing it this way is that it only works with netbeans, if you change your IDE in the future you will have to change all your docblocks. Most, if not all, the current PHP IDE's understand the standard docblocks comment format.
I see. I get what you mean because the standard PHPDocs is always the type first followed by the variable. I will just have modify the netbeans code template for vdoc then.
Update. I just tried using the PHPDocs convention (type varname) and it doesn't work. So I switch back to netbeans convention (varname type) and it works fine. I'm using Netbeans 7.3
It was issues like this that made me swap to PHPStorm :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.