Question

I have file with some PHP code, like this:

    <?php
        foreach ($ideas as $key => $idea) {
            $idea->getIdeaId();
            ?>

            <tr>
                <td>

                </td>
                <td>

                </td>
            </tr>

            <?php
        }
        ?>

Variable $ideas is an array of objects with type KIdea. How can I tell to Netbeans, what $idea in foreach will have type KIdea?

It's need for the code completition.

I have try something like

        <?php
        /**
         *  @param KIdea $ideas Description
         */
        foreach ($ideas as $key => $idea) {

and something like

        <?php
        foreach ($ideas as $key => $idea) {
            /**
             *  @var KIdea Description
             */
            $idea;
            $idea->getIdeaId();

but it's not helpful for the code completion.

Was it helpful?

Solution

With netbeans, you may document your variable with the following syntax :

/* @var $variable MyClass */
$variable = getSomething();

// netbeans will assume that your variable is the MyClass type and will provide code completion.

To auto-complete this string, you may use the following shortcut: type vdoc and press Tab key.

Source for this answer on the Oracles's netbeans php blog.

OTHER TIPS

You have to isolate the code of foreach iteration in a separate function where you can use phpDoc notation as well as type hinting

Some IDEs have implemented a way to document a local variable like that, though to my knowledge the format is not exactly standardized across IDEs, nor in phpDoc syntax (though it's being proposed in PHP-FIG's PSR5 draft).

Use the @var tag to indicate a datatype for the local variable you want to document. The syntax variability in the various IDE implementations is usually the ordering of those values, e.g. is it @var \KIdea $idea or is it @var $idea \KIdea. Since this is not a docblock tied to an OOP code element, it needs the local variable's name in order to tie its info to the right variable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top