Reputation: 4028
I am currently trying to learn JSON and it is kicking my proverbial behind at the moment.
With a normal variable I was able to encode it fine and then echo to see the JSON string.
However I am wanting to encode an object but its not working:
Class tariff
{
var $preset_name;
var $name;
var $net;
var $inclusive;
var $length;
var $data;
function __construct()
{
$preset_name = "Orange-1gb-ECL";
$name = array ("1312" => "Orange 1gb Eclipse");
$net = array ("12312" => "Orange");
$inclusive = array ("1312" => "1GB");
$length = array ("12312" => "12 Months");
$data = array ("12312" => "12p per mb");
}
}
$tariff = new tariff();
$tariff = json_encode($tariff);
echo $tariff;
return 0;
My output is:
{"preset_name":null,"name":null,"net":null,"inclusive":null,"length":null,"data":null}
Ive tried googling and searching on here but can't find my answer!
Help me Obi Wan, your my only hope!
Upvotes: 0
Views: 316
Reputation: 47321
Oh, you have some wrong usages at the constructor,
Should be like this :-
$this->preset_name = "Orange-1gb-ECL"; <-- assign to object property
Instead of
$preset_name = "Orange-1gb-ECL"; <-- assign local variable
Upvotes: 2
Reputation: 1214
Elaborating on another answer,
$preset_name = "Orange-1gb-ECL";
in a member function is actually initializing a local variable to the scope of that function. The $this keyword is your reference to your current instance of your current class, for the purpose of accessing constructs such as properties in the class-instance scope.
Upvotes: 2