I keep getting the following error and I was wondering on how to fix?
Fatal error: Unsupported operand types in C:\wamp\www\tuto\core\Controller.php on line 23
Here is line 23.
$this->vars += $key;
Here is the full code below.
public function set($key,$value=null){
if(is_array($key)){
$this->vars += $key;
}else{
$this->vars[$key]= $value;
}
}
+
can be used in different ways but to avoid complications, only use it on numeric values.
When you didnt initially set $this->vars
to be an array, it won't work (thx to deceze);
see http://codepad.viper-7.com/A24zds
Instead try init the array and use array_merge
:
public function set($key,$value=null){
if (!is_array($this->vars)) {
$this->vars = array();
}
if(is_array($key)){
$this->vars = array_merge($this->vars, $key);
}else{
$this->vars[$key] = $value;
}
}
Examples:
<?php
$test = null;
$t = array('test');
//$test += $t prints the fatal here
$test = array('one');
$test += $t;
// will only print '0 => one'
print_r($test);
$test = array_merge($test, $t);
// will print both elements
print_r($test);
The solution is in the error. You are trying to sum two value that has different types. You are summing array with normal value;
$this->vars += $key;
$key
shouldnt be an array
Or second option;
$this->vars
should be an array
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With