Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a function, that check user language and write it down in a variable. After a time, i come of idea to merge they, so that i need a call the function anytime before the first use of a variable, so i put a call of function inside of var, with a idea, that i would be replace it self. But it does not working, becouse it trying to give me a "Closure Object" back, i think it is a function in clear and not the result :( Here is the important part of code:

$GLOBALS['user_language'] = function()
{
    return get_user_language();
}

function get_user_language()
{
    $user_language = 'en';
    $GLOBALS['user_language'] = $user_language;
    return $user_language;
}

//somewhere in the script
print_r($GLOBALS['user_language']);

I wish to get 'en' out, nothing more.

share|improve this question
This code doesn't make any sense. Your description of what you want to do is not very descriptive either. You can't call a function from a variable. Why don't you just paste your whole script instead of trying to sugar coat a fake version of it that doesn't make any sense? – ChrisG 21 hours ago
What do you need more, if the error is here? For what do you need 200 lines of code more, to know how I set the $user_language variable? It is not the point of question. – BASILIO 21 hours ago
Check my answer. – ChrisG 21 hours ago
@BASILIO, Whitesmiths_style is ok, but TABS need changing to space - TABS are not displayed correctly – Dmi7ry 20 hours ago
show 2 more comments

2 Answers

function get_user_language()
{
    $user_language = 'en';
    $GLOBALS['user_language'] = $user_language;
    return $user_language;
}

$GLOBALS['user_language'] = get_user_language();


//somewhere in the script
print_r($GLOBALS['user_language']);

But this is strange because you set it already in get_user_language() then you pull it again. It would almost create a loop. The proper way would probably be to remove the $GLOBALS['user_language'] = $user_language; from the function.

Hope this answers your question.

share|improve this answer
Thank you very much, this is exactly what I needed. – BASILIO 20 hours ago
Run it and you would be understanding me, why i do so ;) pastebin.com/mNVPk4pi – BASILIO 20 hours ago
Damn, I was too early for dance from happy end. It is not, what i want. I need a call of get_user_language() ONLY i i call $GLOBALS['user_language'] and your example do it any time the script runs – BASILIO 20 hours ago

Just use print_r(get_user_language()) instead of print_r($GLOBALS['user_language']);.

If getting the user's language multiple times would be particularly slow (e.g. a database query would be executed over and over again), you can do something like this:

function get_user_language()
{
    static $user_language = null;
    if ($user_language === null) {
        $user_language = 'en'; // this would be where you make the DB query
    }
    return $user_language;
}

In practice, in a large PHP application, this code would generally be located in a class and would store the value as an object property, so that, for example, the application can cache DB query results for multiple users rather than for only the current one.

share|improve this answer
"Just use print_r(get_user_language())" This is what i wish to avoid. When I forgot anywhere to put it at the start, i spend a lot of time, to search for the error. I wish make it autonomous – BASILIO 20 hours ago
@BASILIO: Is replacing all occurrences of $user_language with get_user_language() really not an option? PHP does have a "magic getter" feature for objects, but not for global variables. – PleaseStand 19 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.