Reputation: 3
Hi I want to know what am I doing wrong, and how can I save class variables into file.
class x{
public $xy=0;
function saveConfig($src){
$classArray = get_class_vars(get_class($this));
$line='';
foreach ($classArray as $k => $v) {
if(is_array($v)){
$line.=$k.'='.implode('|',$v)."\r\n";
}else if(isset($v)) $line.=$k.'='.$v."\r\n";
}
file_put_contents($src,$line);
echo $line;//test
return true;
}
}
This is test class. When running:
$test=new x;
$test->xy=5;
$test->saveConfig('testSrc.txt');
I'll get output "xy=0", but I want it to save/echo changed variable, that's mean "xy=5".
What is wrong with this code, why and how can I correct it ?
Upvotes: 0
Views: 86
Reputation: 1836
Change get_class_vars(get_class($this))
to get_object_vars($this)
.
get_class_vars
returns the default properties of a class. get_object_vars
returns the current properties of an object (an instance of a class).
Upvotes: 2