duankeng2026 2016-03-10 16:05
浏览 442
已采纳

PhpStorm没有检测到php库命名空间

I'm using PhpStorm as current IDE.

The only issue I have with it is that it can't find my php-library.

I've installed this: PHP-JS.

And by install, i mean generated the php-js.so file which I've loaded via php5enmod php-js.

The library works:

// This is a valid namespace AND classname
$context = new JS\Context;
$result  = $context->evaluate("var x = 3; var y = 4; var z = x + y; ++z;");
// will output 8

For some (well, not that strange) reason, PhpStorm can't find the JS namespace and consequently no classes from that namespace.

Is there a way I can tell PhpStorm that I'm actually calling an existing method from a valid class from a valid namespace?

I've tried to make it work using docBlock tags but none seems to match what I need.

  • 写回答

1条回答 默认 最新

  • dragon7088 2016-03-10 17:37
    关注

    There is no way that PhpStorm will read your binary .so file (.dll on Windows) and extract PHP interfaces from there (and by "PHP interface" I mean classes/functions/constants/etc that extension offers during runtime).

    Therefore you would need to do the same as all other binary php extensions would do (e.g. Phalcon's DevTools) -- make stubs files.

    Stub file is a limited PHP version of your classes/methods/functions/etc with (optional) full doc but empty bodies. You can <kbd>Ctrl + Click</kbd> on any standard PHP class (e.g. MySQL) and see yourself how it's done in PhpStorm (IDE will open corresponding stub file in another editor tab) .. or just browse their stub repository directly (thanks @DanFromGermany for the link -- https://github.com/JetBrains/phpstorm-stubs).

    An example of such stub file would be (it covers the code from your example):

    <?php
    
    namespace JS;
    
    class Context
    {
        /**
         * Evaluate your JS code
         *
         * @param string $param JS code to evaluate
         * @return mixed
         */
        public function evaluate($param) {}
    }
    

    Just place such stub files somewhere in your project (or reference any other supported way -- e.g. via PHP | Include paths) -- this code is for IDE only.


    Such stub files can be used by any IDE/editor that can parse source .php files and offer classes/functions from there in code completion. Therefore it makes perfect sense to ask developers of that PHP-JS to provide such stubs automatically (just like Phalcon devs do).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值