I have this PHP class, whose purpose is to fetch some configuration data from a database and store it for later use:
class GSConfig {
private $configurationData;
private $repository;
public function __construct($pRepository) {
$this->repository = $pRepository;
$this->configurationData = $this->InitializeConfiguration();
}
public function InitializeConfiguration() {
$result = $this->repository->configLoadAll();
if ( $result['data'] ) {
$conf_array = $result['data'];
foreach ( $conf_array as $row) {
$code = strtolower($row ['code']);
$value = strtolower($row ['value']);
//if ($value == "true") $value = (bool)true;
//if ($value == "false") $value = (bool)false;
$this->configurationData[$code] = $value;
}
} else {
$this->configurationData = null;
}
print_r($this->configurationData);
}
public function getConfigValue($key) {
$key = strtolower($key);
if ($this->configurationData != null) {
if( isset($this->configurationData[$key])) {
return $this->configurationData[$key];
}
} else if ($this->configurationData == null) {
// If we reach this code, something went wrong
// (TODO: throw Exception)
echo "<p>Tick</p>";
}
}
}
InitializeConfiguration gets the data and stores it as an array in the $configurationData property. This is working as expected as shown by the output of the print_r function.
However, after initializing, if i attempt to read any value from the $configurationData, i get Tick. Somehow the variable becomes null after the Initialization.
The output would be something like:
print_r output:
Array ( [show_cart] => true [order_mail] => order@shop.de [debug_mode] => true [branch_mode] => true [default_branch] => 1 [agb_file] => agb.txt [kat_order] => k_nr [show_rows] => 5 [product_order] => p_produktnr [cost_per_wu] => 0.66 [message_lang] => eng [free_shipping] => true [free_ship_amt] => 25 [special_price] => true [discounts] => true [cat_show_mode] => all [price_att] => ersatzpreis [mwst_att] => mehrwertsteuer [aktionsp_att] => aktionspreis [katalog_mode] => per_branch )
further output:
Tick
Tick
...
Tick
Anyone knows why this is happenning? Btw, my PHP version is 5.3.1
you’re assigning the return value of InitializeConfiguration() but there’s no return statement in this function (defaults to “return null”).