Asked  7 Months ago    Answers:  5   Viewed   48 times

Just curious if there's a way in netbeans to give type hints for regular variables, so that intellisense picks it up. I know you can do it for class properties, function parameters, return types, etc. but I can't figure out how to do it for regular variables. It's something that would really help in situations where you have a method that can return different object types (like a service locator).

ex something like:

/**
 * @var Some_Service $someService
 */
$someService = ServiceLocator::locate('someService');

Where using $someService afterward, netbeans would provide all available methods defined in the class Some_Service.

 Answers

40

A single line is all you need:

/* @var $varName Type_Name */

See this article in the NetBeans PHP Blog: https://blogs.oracle.com/netbeansphp/entry/defining_a_variable_type_in

Note: At least, in version 8.2; The key seems to be:

  • The single asterisk (/* instead of /**).
  • Placing the type after the variable name.
  • Having nothing before and after the type-hinting (except white-space, but even that is not allowed when the comment is not in a single line).
Wednesday, March 31, 2021
 
Kwadz
answered 7 Months ago
29

PHP 7.3 and below does not support typed properties. You could only define a variable as below:

class Sandbox {
    private $connection;

However, to help editors understand your code, you may use a @var tag to document the expected type of the property:

class Sandbox {
    /** @var ConnectorsISandboxConnector */
    private $connection;

Update

PHP 7.4.0

Thanks @Manuel for mentioning the new update, PHP 7.4 now introduces typed properties according to PHP RFC: Typed Properties 2.0.

Property type declarations support all type declarations supported by PHP, with the exception of void and callable. Any class or interface name, stdClass, scalar and compound types, references to parent and own objects are also supported.

class Sandbox {
    public int $id;
    public string $name;
    private ConnectorsISandboxConnector $connection;
}

Note: keep an eye on side effects such as uninitialised state and inheritance strict rules.

Wednesday, March 31, 2021
 
Kemrop
answered 7 Months ago
95
/** @return Control */

for non-static:

/** @return $this */

but it's not documented in phpdoc manual

Saturday, May 29, 2021
 
LoicTheAztec
answered 5 Months ago
32

I think I've solved it. If I encapsulate the nodes inside curly braces as strings, PHPStorm will ignore these.

echo $xml->{'Parent'}->{'ChildElement'};

This has the advantage of being consistent if you encounter an XML tag with a hyphen, for instance. $xml->{'Parent-Node'}

Saturday, May 29, 2021
 
VostanAzatyan
answered 5 Months ago
97

According to Suggested syntax for Python 2.7 and straddling code in PEP 484 which defined type hinting, there is an alternative syntax for compatibility with Python 2.7. It is however not mandatory so I don't know how well supported it is, but quoting the PEP:

Some tools may want to support type annotations in code that must be compatible with Python 2.7. For this purpose this PEP has a suggested (but not mandatory) extension where function annotations are placed in a # type: comment. Such a comment must be placed immediately following the function header (before the docstring). An example: the following Python 3 code:

def embezzle(self, account: str, funds: int = 1000000, *fake_receipts: str) -> None:
    """Embezzle funds from account using fake receipts."""
    <code goes here>

is equivalent to the following:

def embezzle(self, account, funds=1000000, *fake_receipts):
    # type: (str, int, *str) -> None
    """Embezzle funds from account using fake receipts."""
    <code goes here>

For mypy support, see Type checking Python 2 code.

Thursday, June 3, 2021
 
Jeff
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :