0

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!

1
  • That's not how you set class variables ;-) Commented Dec 15, 2011 at 17:03

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

2

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.

2 Comments

but im wanting to add more data later on in an array , this will be going into a pull down menu
I think you misunderstood what I was saying. I was just talking about the difference between $preset_name (local scope) and $this->preset_name (class instance scope).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.