Config_Lite::getBool PHP Method

getBool() public method

returns "on", "yes", 1, "true" as TRUE and no given value or "off", "no", 0, "false" as FALSE
public getBool ( string $sec, string $key, boolean $default = null ) : boolean
$sec string Section
$key string Key
$default boolean return default value if is $key is not set
return boolean
    public function getBool($sec, $key, $default = null)
    {
        if (null === $this->sections && null === $default) {
            throw new Config_Lite_Exception_Runtime('configuration seems to be empty (no sections),' . 'and no default value given.');
        }
        if (null === $sec) {
            if (array_key_exists($key, $this->sections)) {
                if (empty($this->sections[$key])) {
                    return false;
                }
                $value = strtolower($this->sections[$key]);
                if (!in_array($value, $this->_booleans) && null === $default) {
                    throw new Config_Lite_Exception_InvalidArgument(sprintf('Not a boolean: %s, and no default value given.', $value));
                } else {
                    return $this->_booleans[$value];
                }
            }
        }
        if (array_key_exists($key, $this->sections[$sec])) {
            if (empty($this->sections[$sec][$key])) {
                return false;
            }
            $value = strtolower($this->sections[$sec][$key]);
            if (!in_array($value, $this->_booleans) && null === $default) {
                throw new Config_Lite_Exception_InvalidArgument(sprintf('Not a boolean: %s, and no default value given.', $value));
            } else {
                return $this->_booleans[$value];
            }
        }
        if (null !== $default) {
            return $default;
        }
        throw new Config_Lite_Exception_UnexpectedValue('option not found, no default value given.');
    }

Usage Example

示例#1
0
<?php

require_once 'Config/Lite.php';
$config = new Config_Lite();
// read config file
$config->read('phpmussel.ini');
// get value from config file
var_dump($config->getBool('general', 'cleanup'));
var_dump($config->getBool('general', 'disable_cli'));
// set value in config file to false
$config->set('general', 'cleanup', false);
$config->set('general', 'disable_cli', true);
// get current value (not yet saved to the config file)
var_dump($config->getBool('general', 'cleanup'));
var_dump($config->getBool('general', 'disable_cli'));
// save new value to config file
$config->save();
// read updated config file
$config->read('phpmussel.ini');
// get saved value from config file
var_dump($config->read('phpmussel.ini')->getBool('general', 'cleanup'));
var_dump($config->read('phpmussel.ini')->getBool('general', 'disable_cli'));