What are the best practices for managing complex program logic and multiple flags in PHP without relying heavily on global variables?

Managing complex program logic and multiple flags in PHP without relying heavily on global variables can be achieved by encapsulating the logic within a class and using class properties to store the flags. This approach helps in keeping the code organized and makes it easier to manage the flags within the context of the class.

class FlagManager {
    private $flag1;
    private $flag2;
    
    public function __construct() {
        $this->flag1 = false;
        $this->flag2 = false;
    }
    
    public function setFlag1($value) {
        $this->flag1 = $value;
    }
    
    public function setFlag2($value) {
        $this->flag2 = $value;
    }
    
    public function getFlag1() {
        return $this->flag1;
    }
    
    public function getFlag2() {
        return $this->flag2;
    }
}

// Example of using the FlagManager class
$flagManager = new FlagManager();
$flagManager->setFlag1(true);
$flagManager->setFlag2(false);

if ($flagManager->getFlag1() && !$flagManager->getFlag2()) {
    echo "Flag 1 is set and Flag 2 is not set.";
}