PhpStorm无法识别类别名 [英] PhpStorm does not recognise class aliases

查看:890
本文介绍了PhpStorm无法识别类别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看过了这个问题 ,但不幸的是没有答案.

I've taken a look at this question, but unfortuently there is no answer.

我正在使用PhpStorm,并且正在构建一个加载了composer项目的项目.我正在努力做到这一点,以使PhpStorm可以在我按住Ctrl键并单击它们时识别我的类/函数,特别是对于通过composer安装的项目.

I'm using PhpStorm and I am building a project with a composer project loaded. I'm trying to make it so that PhpStorm recognises my classes/functions when I ctrl+click on them, specifically, for the projects installed via composer.

我曾尝试在Settings/Directories中更改Source Folders,但仍然无法正常工作.

I have tried changing the Source Folders in Settings/Directories but I still cannot get this to work.

我正在使用 require __DIR__ . '/vendor/autoload.php';加载我的作曲家依赖关系.

I am using require __DIR__ . '/vendor/autoload.php'; to load my composer dependancies.

我需要什么来使PhpStorm识别通过作曲家加载的文件中的声明?

为支持我的问题,下面是一些图片:

To support my question, here is some pictures:

我的test.php文件:(注意它无法识别我的班级): project/test.php

My test.php file: (notice how it can't recognise my class): project/test.php

通过composer添加并使用自动加载器安装的项目: project/vendor/braintree/braintree_php/lib/Braintree/Subscription.php

A project added via composer and installed using autoloader: project/vendor/braintree/braintree_php/lib/Braintree/Subscription.php

我的composer.json文件:project/composer.json

My composer.json file: project/composer.json

推荐答案

从技术上讲,该软件包没有定义Braintree_Subscription类(我的意思是正确"的定义).您的第二张屏幕截图显示了\Braintree名称空间中Subscription类的定义.

Well ... technically that package has no Braintree_Subscription class defined (I mean -- "proper" definition). Your 2nd screenshot shows definition of Subscription class from \Braintree namespace.

这是该文件: https://github.com. com/braintree/braintree_php/blob/master/lib/Braintree/Subscription.php

请注意最后一行-调用class_alias()函数时在此提到未定义"类:

Notice the very last line -- that "undefined" class is mentioned there when calling class_alias() function:

class_alias('Braintree\Subscription', 'Braintree_Subscription');

您的PhpStorm似乎不理解此类定义.

It looks like your PhpStorm does not understand such class definition.

以下是PhpStorm的问题跟踪器的票证: WI-11936 -因此门票已于2016年11月3日确定(基本上是2天前).该修补程序(支持类别名)将在下一个主要版本 2016.3 中提供.

Here is the ticket from PhpStorm's Issue Tracker: WI-11936 -- accordingly to the ticket it has been fixed on 3rd of November 2016 (2 days ago, basically). The fix (class alias support) will be available in next major version, which is 2016.3.

您可以等到它正式发布(大约在本月末),或者尝试下一个 EAP或RC构建(观看其

You may wait till it will be officially released (approximately end of the month) or try next EAP or RC build (watch their blog or twitter for announcements).

现在(对于当前和过去的稳定版本:2016.2和更早的版本),解决方案是在代码中使用真实"类名(\Braintree\Subscription)而不是别名(Braintree_Subscription).

Right now (for current and past stable versions: 2016.2 and older) the solution is to use "real" class name (\Braintree\Subscription) instead of alias (Braintree_Subscription) in your code.

但是在任何情况下:最正确的方法是使用真实的类来使用真实的类-在需要某些旧版/兼容性支持以及其他边缘情况的情况下,请保留类别名.

But in any case: using real class in your own fresh code name is the most correct way -- leave class aliases for situation where some legacy/compatibility support is required and other edge cases.

PS 这与Composer无关-完全没有.

P.S. This has nothing to do with Composer -- nothing at all.

这仅仅是代码示例-出于某种原因(可能是 遗留/兼容的原因..或者对于PHP新手来说更容易,不知道),他们正在使用类别名而不是真实的类名而PhpStorm无法理解此类别名.

It's just the code examples -- for whatever reason (possibly legacy/compatibility reasons .. or easier for new to PHP users, do not know) they are using class aliases instead of real class names and PhpStorm does not understand such aliases.

这篇关于PhpStorm无法识别类别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆