Class constant not found via self::CONSTANT

Running 2019.3.1 on Windows 10.

It seems that class constants are not recognized when accessed by keyword self.

Setting visibility to constant does not change the behaviour.
Replacing self::CONSTANT with MyClass::CONSTANT helps IDE recognize the constant correctly (see showConstantExplicit() below).

Any thoughts on this?

<?php
class MyClass
{
const CONSTANT = 'constant value';

function showConstant() {
echo self::CONSTANT . "\n"; // Constant 'CONSTANT' not found in
}

function showConstantExplicit() {
echo MyClass::CONSTANT . "\n"; // Highlights and connects correctly
}
}
9 comments
Comment actions Permalink

That's interesting, I was not able to reproduce the issue, both ways are working fine for me.

Does it also happen in a brand new project with only one file added?

Out of curiosity, did you try to Invalidate caches already? (yes, I know that it is too a super-generic request but nevertheless)

0
Comment actions Permalink

It seems this was a temporary problem.
After a couple restarts, the constants are working with both notations.

I tried initially to both restart IDE and invalidate caches but did not try to open an empty project.
Only checked on plain class file ( code above ) in the current project.

Anyway, case closed for now.




0
Comment actions Permalink

Have same issue

PhpStorm 2019.3.3, Ubuntu 19.10

  • Add to Phrasebook
     
    • No word lists for English -> Russian...
       
    • Create a new word list...
  • Copy
0
Comment actions Permalink

Max, could you please tell more about your scenario? What exact issue do you have?

A screenshot or code example are warmly welcome.

0
Comment actions Permalink

I have same error like Antti Pikkarainen described. No restarts help me.

Everything seems fine during a coupe of seconds after loading a project, class constants have "in use" color and I think IDE could find their usage. But after a moment their color changes to grey ("unused element") and the "Show usages of class constant" command says "No usages found in all places".

This issue is observed for the single class of my project. All is fine with constants for all other classes.

  • Add to Phrasebook
     
    • No word lists for English -> Russian...
       
    • Create a new word list...
  • Copy
0
Comment actions Permalink

Could you please share a code sample where this issue is clearly reproducible?

0
Comment actions Permalink

Had the same issue. Did "File -> Invalidate caches / Restart..." and problem gone. But that was very weird :)

 

PhpStorm 2019.3.3
Build #PS-193.6494.47, built on February 12, 2020
Runtime version: 11.0.5+10-b520.38 x86_64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.13.6
GC: ParNew, ConcurrentMarkSweep
Memory: 1981M
Cores: 8
Registry: run.processes.with.pty=TRUE
Non-Bundled Plugins: BashSupport, com.tang, de.espend.idea.php.annotation, fr.adrienbrault.idea.symfony2plugin, mobi.hsz.idea.gitignore, ru.adelf.idea.dotenv

 

-- Removed license and subscription lines :) JB can get them from my account, if required.

1
Comment actions Permalink

Thanks a lot, Anton !
That was really helpful !

I think there's no need to share the code sample as well as to dig deeper for resolving this issue since it seems an occasional bug (maybe power loss or smth like that)

0
Comment actions Permalink

I had the same issue and File > Invalidate cache / restart, fixed it for the time being.

PhpStorm 2019.3.3
Build #PS-193.6494.47, built on February 12, 2020
Runtime version: 11.0.5+10-b520.38 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Windows 10 10.0
GC: ParNew, ConcurrentMarkSweep
Memory: 1976M
Cores: 4
Registry: run.processes.with.pty=TRUE, search.everywhere.configurations=false, search.everywhere.toolwindows=false
Non-Bundled Plugins: FrameSwitcher, com.neon.intellij.plugins.gitlab, com.ppolivka.gitlabprojects, krasa.nonProjectFilesUnlocker

0

Please sign in to leave a comment.