English 中文(简体)
JetBrains WebIDE: PHP variable type hinting?
原标题:

Is there a way to hint WebIDE that a variable has some type? I have to iterate an array of objects, and there s no auto-completion available. This helps in ZendStudio:

/* @var ClassName $object */

I know there s a feature in JetBrains to declare an array of objects:

/**
 * @return ClassName[]
 */

But this works only with function s return type.

最佳回答

/* @var ClassName $object */ is a non-valid PHPDOC comment and is not parsed in the current version of Web IDE. Use double asterisks to make it work:

/** @var ClassName $object */

Also, you can annotate $array in foreach($array as $var) with /** @var ClassName[] $array */ and $var type will be deduced automatically.

问题回答

As already pointed out, PhpStorm will use regular phpdoc blocks:

/** @var ClassName $object */

However, since 2.1 it also supports Netbeans/Eclipse/Zend @var annotations:

/* @var $object ClassName */

Please note the comment starts with /* rather than /** (thus it won t show up if you generate actual documentation with phpdoc). Also, the arguments are swapped, though PhpStorm accepts any order:

/* @var ClassName $object */

Last but not least, they can precede almost any arbitrary line of code (technically, phpdoc blocks are restricted to certain items).


Edit: as of 2019, Netbeans/Eclipse/Zend @var annotations seem to be mostly abandoned. NetBeans 11 no longer supports them and in general they aren t supported by other IDEs. I suggest to use the other syntax.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签