autocomplete with object in $GLOBALS array

hi

one of the CMS I use have some objects stored in $GLOBALS array.

Is there any way to make autocomplete with such object?

This doesn't work: :(

/** @var $GLOBALS['TYPO3_DB'] t3lib_DB */
$GLOBALS['TYPO3_DB']->...autocomlpete here....

1
9 comments

Hi Krystian,

Currently it is not possible to give type hint to a specific array element. There is already a feature request on Issue Tracker, but I cannot find it right now.

You are using $GLOBALS to access global variables from within functions/class methods. Can you use global $varname; approach instead? Have a look at this dummy example:

<?php
class ABCClass {
    public function doit()
    {
        echo "done it!\n";
    }
}

$foo_bar_foo = 123;

function yes ()
{
    /** @var $foo_bar_foo ABCClass */
    global $foo_bar_foo;

    $foo_bar_foo->doit(); // Code completion/suggestion works here
}

0

No - this is not possible to use it like that.
The arrray of objects inside the $GLOBALS array is build during init of the CMS. No way to change that.

0

/** @var $GLOBALS['TYPO3_DB'] t3lib_DB */
$GLOBALS['TYPO3_DB']->...autocomlpete here....

Where are you trying to use this type hinting -- in your own code or inside CMS classes (i.e. you are developer of that CMS)?

If first -- then I don't really see why it is not possible. In CMS classes you access global variables via $GLOABALS (you do not need to edit those files anyway, unless you are trying to fix some bugs) and in your own code via global keyword.

<?php

function check_it()

{
    global $boom;


    echo $boom;
}


function init()
{
    $GLOBALS['boom'] = 'clever';
}


init();
check_it();

You can even mix them within the same function if necessary.

function check_it()
{
    global $boom;
    echo $boom, "\n";

    $GLOBALS['boom'] = 'indeed';
    echo $boom, "\n";
}

0

Well is works but IMO adding new line
global $boom;
just to get autocomplete is not a clean solution.

I can not accept that in my code.

0

Well, I just suggested you a way how it can be achieved in current version of PhpStorm. It is up to you to use it or to wait until type hinting for individual array element will be implemented.

0

Thanks for your time Andriy !

0

Looks like it's still the same.
For now the most elegant way to use autocomplete is this: `global $boom; $boom...` in one line.

0

@Bence Szalai

Have a look at deep-assoc-completion plugin -- it has quite a few mechanics that may work with $_GLOBALS as well (thankfully, I never had to use $_GLOBALS myself).

0

@Andriy Bazanov

I did a quick test. For me, looks like it does not work with $_GLOBALS, but very usefull plugin nevertheless, so thanks for the recommendation!

0

Please sign in to leave a comment.