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
}
}
3
15 comments

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

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
Avatar
Permanently deleted user

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
1

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

A screenshot or code example are warmly welcome.

0
Avatar
Permanently deleted user

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

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

0

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
Avatar
Permanently deleted user

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

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

Same issue on PhpStorm 2022.2.3
Build #PS-222.4345.15, built on October 5, 2022
Runtime version: 17.0.4.1+7-b469.62 amd64
VM: OpenJDK 64bot Server VM by JetBrains s.r.o

Ubuntu 22.04.2 LTS - Jammy

1

Could you please share a reproducible code sample?

0

Same problem (Ubuntu 22.04.1)
PhpStorm 2022.3.3  (also same problem with 2022.3.2)
Set to PHP 8.1, runs perfectly with `php Test.php`

 

The code, if that helps:

<?php

class Test {
    const TEST_CONST = 100;

    public static function hello($p) {
        if ( $p > self::TEST_CONST ) {
            echo "Too big\n";
        }
    }

}

Test::hello (1000);

exit;

 

 

0

Hm, I was not able to reproduce it on the latest IDE version (2023.1):
Would it be possible to update your installation and check if it is still an issue?

0

Ok I think I know what the problem is, and the "...not found in <nothing>" alert box seems to confirm that.

This test file and the actual one where opened outside the project scope (they were above the project in the dir tree).

Just a guess: to resolve a class, PhpStorm uses its "include" rules, even if the symbol used and its declaration are in the same currently edited file, even within the same class via "self".

The file is outside the project scope, so ... is that the intended bahavior?

(Btw, will upgrade to 2023.2, or maybe 2023.1.2. Usually the '.1' version has still a lot to be fixed)

0

Thank you for the clarification, I was able to reproduce the issue in this way. Turns out, this is a known problem, here is a ticket about it:

https://youtrack.jetbrains.com/issue/WI-65601/Inspection-for-Undefined-Class-Constant-not-detecting-self-static-in-scratch-php-file

It is mostly about scratch files but the root cause should be the same.

0

Please sign in to leave a comment.