Scope of defined constants visibility

In my project I have MVC architecture and in class Application I define some constants like AUTH and AUTH_ID.

I can use this constants in other files - controllers, etc, IDE recognizes them as defined constants and I can move to constant declaration by pressing Ctrl+B, but in files outside Application.php (where my class defined, where constants defined)   I do not have them in suggestion list.

So my question is: Is there any way to get my constants throughout all project in suggestion list?



Attachment(s):
controller.jpg
application.jpg
5 comments

Hi there,

It works fine for me here in my projects.

1) Did it worked before and now stopped? Try "File | Invalidate Caches..."
2) Do you use namespaces?
3) How those constants are defined?
4) You do not have them in "suggestion list" .. but you you actually type the full name manually -- does it work (I mean: does PhpStorm shows an error; can it navigate to declaration in such case)?
5) Please provide some sample project (as simple as possible, if it is yours ..or full one if it is open source) that reproduces the issue. So far I have no real clue why it does not work for you.

0
Actually, this is a known issue: http://youtrack.jetbrains.com/issue/WI-11390 Constants not defined globally are not available outside that function or class.What we've done is create a "dummy" file that we put constants in (name only, defined as  null). Sucks, but JetBrains said the issue won't be fixed.
0

Hello, Thank you for answer

1) Never wodked. Doesn't help.
2) No
3) With define instruction
4) Everything is works, except suggestion
5) Example attached



Attachment(s):
example.zip
proj.jpg
0

Oh, this is sad. I found this solution on stack overflow, but it means that both constants will shown in "go to declaration" menu. Well, it's better than nothing -)



Attachment(s):
const_solution.jpg
0

Thanks for the project.

Keith is correct -- it is WI-11390.

If you do not want to change your code -- use workaround suggested (it is for IDE only).

If possible -- change your code (will be beneficial for all):
A) define constants in global namespace (I mean: outside of class). For example:

<?
define('AUTH_USER', 'Pavel');


class myClass {
    function __construct() {
        
    }
}


A "trick" with include/require will also work just fine, e.g. (based on your example)
lib.php

<?
class myClass {


    function __construct() {
        require "./defines.php";
    }

}

defines.php

<?php
define('AUTH_USER', 'Pavel');



B) Maybe you can convert it into class constants:
lib.php

<?
class myClass {
    const AUTH_USER = 'Pavel';
    
    function __construct() {
        
    }
}

index.php

<?
include 'lib.php';

$obj = new myClass();
myClass::AUTH_USER



C) Or just class variables (so you can alter them as required) -- be it real variable .. or magic one (just treat them as constants in your code -- do not assign new value anywhere else). But this means you have to change even more code.

<?
class myClass {
    public static $AUTH_USER = '';
    
    function __construct() {
        self::$AUTH_USER = 'Pavel';
    }
}

0

Please sign in to leave a comment.