Netbeans Auto-Complete Not Working For Custom PHP Class

送分小仙女□ 提交于 2020-01-05 08:46:14

问题


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?


回答1:


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.




回答2:


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();

    }
}


来源:https://stackoverflow.com/questions/9429874/netbeans-auto-complete-not-working-for-custom-php-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!