5 Replies Last post: May 13, 2013 9:52 PM by Andriy Bazanov  
jazzfog Newbie 36 posts since
Jan 7, 2011
Currently Being Moderated

May 13, 2013 10:15 AM

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?

Attachments:
Andriy Bazanov Master 3,991 posts since
May 28, 2010
Currently Being Moderated
May 13, 2013 12:54 PM in response to: jazzfog
Re: Scope of defined constants visibility

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.

Andriy Bazanov Master 3,991 posts since
May 28, 2010
Currently Being Moderated
May 13, 2013 9:52 PM in response to: jazzfog
Re: Scope of defined constants visibility

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';
    }
}

Keith Davis Apprentice 676 posts since
Aug 23, 2010
Currently Being Moderated
May 13, 2013 6:43 PM in response to: jazzfog
Re: Scope of defined constants visibility
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.

More Like This

  • Retrieving data ...