未声明变量的类型注解

Type annotations of undeclared variables

我继承了一个使用自制模板系统的项目,其工作原理如下:

# main code

include_template('page.php', array('user' => $user, 'account' => $account));

# template 'engine'

function include_template($path, $vars) {
    extract($vars);
    include $path;
}

模板"page.php"是一个普通的php/html文件,例如:

 <h1><?= $user->name ?></h1>
 <p>Balance: <?= $account->balance ?></p> etc

由于extract,传递给include_template的变量在page.php中可见,但是IDE(phpstorm)对它们一无所知,所以它将它们突出显示为未定义并且不提供自动完成等。有没有办法在 page.php ("bare" php/html 文件)中注释未声明的变量,以便 IDE可以看到吗?

您必须在 page.php 中声明它。请参见以下代码。它适用于 PHPStorm。

<?php
/**
 * @var User $user
 * @var Account $account
 */
?>
<h1><?= $user->name ?></h1>
<p>Balance: <?= $account->balance ?></p> etc