开发者

Class instance variables becoming null in php

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']);
        /开发者_如何学C/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").


I suspect it's because in your constructor, you assign $this->configurationData to the return value of InitializeConfiguration(). But it doesn't look like InitializeConfiguration() returns anything. I think the easiest way to fix this is to change:
$this->configurationData = $this->InitializeConfiguration();

to:
$this->InitializeConfiguration();


The problem is that you're assigning the return value of InitializeConfiguration to $this->configurationData inside your constructor, which will be null.

The following remains sound advice:

Strongly suspect your error is arising from your using the incorrect comparison operator.

Never, ever do this in PHP:

if ($this->configurationData == null)

When testing against null, false or 0, always use === which checks that both the values and types of each variable are the same. Simply using == will cast one side to the type of the other before the conversion.

The following are all true when using the == operator:

null == 0;
array() == null;
null == false;
false == 0;

You also shouldn't be using else if ($this->configurationData == null) in getConfigValue; use a simple else since your intent seems to be to cover all other cases.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜