0

Hello I'm pretty new in programming. I need to solve this problem in php but the solution in any different language will be great. I tryied to solve it with if statement but if condition is changed the variable is gone. Easy example for better understanding.

// possible conditions ( 'cond1', 'cond2', 'cond3', 'cond4','cond5' )
// conditions can be called randomly

I would like to have somethng like this:

$variable = 'off';
since ( $condition == 'cond2' )
    $variable = 'on';
until ( $condition == 'cond4' )

The goal is to switch variable 'on' in the 'cond2' condition and hold it on when the others conditions are changing independently on their order until condition is changed to 'cond4' and variable is switched back to 'off'.

Thanks for any suggestions.

Jakub
  • 103
  • 1
  • 6
  • If you want to set $variable based on anything but cond4 being selected, why not used the Does Not operator? http://www.php.net/manual/en/language.operators.comparison.php – Aravona Jun 02 '14 at 14:22
  • Thanks for your interest Aravona. I'm afraid that it will not help in this case. As you see the $variable has 2 states as a switch and I need all conditions in both states except two conditions turning the switch 'on' and 'off'. But I'm beginner and maybe not understand your concept properly. – Jakub Jun 03 '14 at 19:26

1 Answers1

0

I don't think your current concept is realizable in PHP as you cannot listen to variables, you need to actively get notified. So one scenario with the same solution but a different concept would be

class Condition {

    private $value;
    private $variable = false;

    public function setCondition($new_value) {
        $this->value = $new_value;
    }

    public function getCondition() {
        return $this->value;
    }

    public function isVariableSet() {
        return ($this->variable === true); //TRUE  if $this->variable is true
                                           //FALSE otherwise
    }

}

Now in the method setCondition(...) you can listen and actively set the variable.

    public function setCondition($new_value) {
        switch ($new_value) {
            case 'cond2':
                $this->variable = true;
                break;
            case 'cond4':
                $this->variable = false;
                break;
        }
        $this->value = $new_value;
    }

With this you can use it like the following

$foo = new Condition();
$foo->setCondition('cond1');
var_dump( $foo->isVariableSet() ); //FALSE

$foo->setCondition('cond2');
var_dump( $foo->isVariableSet() ); //TRUE

$foo->setCondition('cond3');
var_dump( $foo->isVariableSet() ); //TRUE

$foo->setCondition('cond4');
var_dump( $foo->isVariableSet() ); //FALSE

Or in your case:

$conditions = array( 'cond1', 'cond2', 'cond3', 'cond4','cond5' );

$cond = new Condition();
foreach ($conditions as $i => $condition) {
    $cond->setCondition($condition);
    if ($cond->isVariableSet() == true) {
        $toggle = 'on';
    }
    else {
        $toggle = 'off';
    }
    $results[$condition] = $toggle.' ;  ';
}

If you don't create the instance of Condition outside the loop, you gain nothing as you create a new object everytime and no state stays. However, exactly that is required.

You can also do this via array_map() and save the foreach()

$conditions = array( 'cond1', 'cond2', 'cond3', 'cond4','cond5' );

$cond = new Condition();
$results = array();

$setCondGetVariable = function($condition) use($cond) {
    $cond->setCondition($condition);
    if ($cond->isVariableSet() == true) {
        $toggle = 'on';
    }
    else {
        $toggle = 'off';
    }
    return $toggle.' ;  ';
};

$results = array_map($setCondGetVariable, $conditions);
kero
  • 10,647
  • 5
  • 41
  • 51
  • Thanks for nice looking and seemingly elegant solution. But it doesen't work for me. I'll post my code for check if there something wrong. – Jakub Jun 05 '14 at 19:05