简体   繁体   English

当在 PHP 中使用 ::class 但在 Visual Studio Code 中未使用 class 时收到警告的扩展

[英]Extension to get warned when ::class is used in PHP but class is not used in Visual Studio Code

Is there any PHP extension for Visual Studio Code that can warn you in case you are doing::class in PHP but the class is not use d on top of the file?如果您在 PHP 中执行 ::class 但 class 未在文件顶部use d,Visual Studio Code 是否有任何 PHP 扩展可以警告您?

Let me show you a simple dummy example that proves my point:让我向您展示一个简单的虚拟示例来证明我的观点:

<?php

declare(strict_types=1);

namespace Src\Users;

class ExampleClass
{
    protected string $anotherClass = AnotherExampleClass::class;
}

If you copy and paste this code on a PHP project in VSCode it won't highlight in red the $anotherClass declaration, even though AnotherExampleClass is not use d after the namespace line and is a class that is not within the same folder as ExampleClass .如果您将此代码复制并粘贴到 VSCode 中的 PHP 项目,它不会以红色突出显示$anotherClass声明,即使AnotherExampleClass未在命名空间行之后use并且是一个 class 与ExampleClass不在同一文件夹中。

This won't even work for me with PHP Intelephense and PHP IntelliSense extensions enabled.这甚至对启用 PHP Intelephense 和 PHP IntelliSense 扩展的我都不起作用。

Thanks in advance.提前致谢。

if you want the class to be added automatically you can use spl_auto_register.如果你想自动添加 class,你可以使用 spl_auto_register。 Because sometimes it can be difficult to track the location of the files.因为有时很难跟踪文件的位置。 for ex.例如。

spl_autoload_register(function ($class) {
global $dir;
if (file_exists($dir. $class . ".php")) {
    include $dir. $class . ".php";
} elseif (file_exists($class . ".php")) {
    include  $class . ".php";
}

}); });

You can check again with  is_file or file_exists .

No syntax errors in this file.此文件中没有语法错误。 Namespace not mapped to single directory ie need analyze all project and vendor classes(try all registered auto-loaders).命名空间未映射到单个目录,即需要分析所有项目和供应商类(尝试所有已注册的自动加载器)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM