0

I have a loop which will make calls to the function. Variables are defined (and reassigned on each iteration) in the first loop which are required for the function to function.

Loop:

if ($something) {
    while (!$recordSet->EOF) {
       $variable1 = TRUE;
       $variable2 = FALSE;
       ...
       function1()
    }
}

Function:

function function1() {
   if ($variable1 && !$variable2) {
      ...
   }
}

The variables will have boolean values, and the environment is limited to PHP 4.

I'm currently considering using global $variable1; in the while loop and function1, but I know globals are almost always frowned upon.

Usually I'd use define("variable1","a value"), but the values will be changed multiple times.

Any suggestions, or is global defining the best solution in this case?

Thanks.

EDIT: Totally forgot to mention. This file is actually a spaghetti legacy code, and function1 is called in a hundred different places, all with varying bits of information. Otherwise, I would have used arguments.

2

1 Answer 1

1

In the main scope, define global $variable1 and global $varible2.

Also do it in the function. But this is the worst solution. You will confuse, if these variables changes somewhere else.

The best way I think is to refactore your code, and pass varables as parameters.

The other solution could be create a class for these 2 variables, and set/get them statically.

class variablesPlaceHolder {

    private static $variable1;

    private static $variable2;

    public static function getVariable1() {
        return self::$variable1;
    }

    public static function getVariable2() {
        return self::$variable2;
    }

    public static function setVariable1($variable1) {
        self::$variable1 = $variable1;
    }

    public static function setVariable2($variable2) {
        self::$variable2 = $variable2;
    }
}

And the include this class in file, where you want to use them, and call variablesPlaceHolder::setVariable1(anyValue) variablesPlaceHolder::getVariable1()

Sign up to request clarification or add additional context in comments.

5 Comments

Yeah, this is what I was currently leaning towards. It's not ideal, but it will do the job. Problem with refactoring is that the page is 3k+ lines, about 40 are my own, and countless other files rely on it (report generating script). Think this will have to be it.
I edited my comment, please check it. With this placeholder class you cant overwrite directly the values of variables.
The variables need to be able to change value though, on each iteration of the loop they may change.
But you can. Just call the variablesPlaceHolder::setVariable1(anyValue); in the loop, and variablesPlaceHolder::getVariable1(); in the function.
Ah, I guess I misread your previous comment. Thanks, I'm going to stick with globals for now, but will look into this when I (one day!) get approval to refactor the whole thing. Marked as answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.