Netbeans Auto-Complete Not Working For Custom PHP

2019-08-03 01:16发布

I've got the following class in a Zend Framework project:

<?php

/**
 * User's class
 *
 * This class should be responsible for all 
 * 
 * @author     Steve Davies
 * @copyright  2012
 * @version    SVN: $Id$
 */
class Api_Admin_Users extends Api_Core
{

    /**
     * Class Constructor
     *
     * @return void
     */
    public function __construct() {
        parent::__construct();
    }

    /**
     * Get User's name
     *
     * This returns the user's name
     *
     * @return void
     */
    public function new() {

        $user = self::_instance()->_em->getRepository('UserManagement\Users')->find('1');
        echo $user->getFullName();

    }
}

However when I try and use code hinting on $user->getFullName();, it doesn't work.

Using the following trick from here, it works:

/**
 * Get User's name
 *
 * This returns the user's name
 *
 * @return void
 */
public function new() {

    /* @var $user \UserManagement\Users */
    $user = self::_instance()->_em->getRepository('UserManagement\Users')->find('1');
    echo $user->getFullName();

}

But, I don't want to have to include that comment line everytime I instantiate the object. When I try to move this to the Class definition - or even the method definition, it fails to work.

Can anyone provide an answer for this?

2条回答
SAY GOODBYE
2楼-- · 2019-08-03 01:48

Create a method in Api_Admin_Users to access the repository and add the type hint there. This will benefit all methods in the class. As long as the methods in the repository are type-hinted correctly, you're all set.

class Api_Admin_Users extends Api_Core
{
    /**
     * Class Constructor
     *
     * @return void
     */
    public function __construct() {
        parent::__construct();
    }

    /**
     * Get the repository
     *
     * @return \UserManagement\Users
     */
    public static function getRepository() {
        return self::_instance()->_em->getRepository('UserManagement\Users');
    }

    /**
     * Get User's name
     *
     * This returns the user's name
     *
     * @return void
     */
    public function new() {
        $user = self::getRepository()->find('1');
        echo $user->getFullName();

    }
}
查看更多
Explosion°爆炸
3楼-- · 2019-08-03 01:55

PHP is a dynamic language and as such it is not trivial to infer variable types from static code analysis (like it is in Java for example).

It's especially difficult with factory methods like yours getRepository('UserManagement\Users').

NetBeans currently has no way of knowing how to translate the function argument to the type of returned variable (unless you're satisfied with some parent class from which all subclasses returned by that factory derive). Unfortunatelly vdoc's are the only way to deal with such cases.

查看更多
登录 后发表回答