The $GLOBALS array members can't get evaluated inside functions

I have a code:

$GLOBALS['MySQL'] = new BxDolDb();
function db_list_tables( $error_checking = true )
{
$GLOBALS['MySQL']->setErrorChecking ($error_checking);
return $GLOBALS['MySQL']->listTables();
}

The methods setErrorChecking and listTables cannot be found inside a function (when I try to track them with Ctrl + Mouse).
The array $GLOBALS should be accessible from inside any function but it doesn't seem so.
Is there any workaround here?
Thank you for your help.
2 comments
Comment actions Permalink

Hi there,

PhpStorm in general does not track the types of individual array elements, especially the way it is used here.

With the coding style that you are showing here (procedural; relies on globals) the best I can suggest is to use intermediate variable with manual type hint, something like this:

function db_list_tables( $error_checking = true )
{
/** @var BxDolDb $db */
$db = $GLOBALS['MySQL'];

$db->setErrorChecking ($error_checking);
return $db->listTables();
}
0
Comment actions Permalink

Thank, Andriy.

You say that PhpStorm does not track the types of individual array elements, but this is not true for the global scope because the following statement has no problem in the global scope:

$GLOBALS['MySQL']->setTimezone(getParam('site_timezone'));

While your suggestion is reasonable, I would not want to change all the pieces of code where the $GLOBALS array is used inside a function.
Is there a way to create an annotation for an array member so PhpStorm could just rely on that annotation?

0

Please sign in to leave a comment.