Welcome Guest, Not a member yet? Register   Sign In
Message: Creating default object from empty value
#1

[eluser]Mainboard[/eluser]
hello everyone, i have a little warning and i not understand very well how to fix that, i set this variable
Code:
public $settings;
and this function
Code:
public function set_session_data() {

  $mcb_data = $this->db->get('mcb_data')->result();

  foreach ($mcb_data as $data) {

   $this->settings->{$data->mcb_key} = $data->mcb_value;

  }

}
and works very well, but since i migrate to PHP 5.4 this warning appears:
Severity Warning
Message: Creating default object from empty value
So i undertand that the reason for this is maybe because the version of PHP had a E_STRICT error reporting constant. The PHP manual states "Enable to have PHP suggest changes to your code which will ensure the best interoperability and forward compatibility of your code.". Thus it's recommendable to enable strict PHP errors.
So i want if it could to fix this in the code to get more clear code
#2

[eluser]PhilTem[/eluser]
As long as you won't initialize all your setting keys in the class header like you did for public $settings; you will always get that error (since your PHP interpreter actually doesn't know the variables you are accessing in your foreach-loop exist.
If you want to life without these PHP-errors, you can either specify all keys in the class header with something like

Code:
public $setting_key_1;
public $setting_key_2;

which will only be good if you have little to no keys and if they don't change or might not be extended over time.

On the other hand, you could just use an array

Code:
private $_items = array();

in your header which will keep all your setting items. However, you will then have to access each item with

Code:
$this->_config['settings_key_1']

which can be exhausting over time. Yet, there is a way to change this and still let you use $this->settings_key_1.
To not write all the code for you (since I don't know if it will suit your needs) I will just give you three keywords and an URL:
Magic Getter and Setter
#3

[eluser]CroNiX[/eluser]
Why would you loop over the array only to assign each value to a property, which is another array? Just assign the array to the property.

Code:
$this->settings = $this->db->get('mcb_data')->result();

Code:
echo $this->settings->some_key;
#4

[eluser]Unknown[/eluser]
You're seems to be using the "myBaseClient" open source utility, in that case I've solved that issue initializing
the public property called setting inside the constructor, there hasn't been any in this case so I've created one to achieve this.
Code:
function __construct()
{
  parent::__construct();
  $this->settings = new stdClass();
}

after that, everything went fine!.
greetings.
:-)




Theme © iAndrew 2016 - Forum software by © MyBB