V
V
Vladimir Shiklgruber2015-06-23 14:53:22
PHP
Vladimir Shiklgruber, 2015-06-23 14:53:22

Why does post_geson refuse to work?

Hello. Here I am doing a class for robots with data. Everything works except json_decode function

class data {
var $data;


    function get($data) {
        if (isset($_GET[$data])) {
            $data = $_GET[$data];
            $data = trim(htmlentities($data));
            $this->data = $data;
        }
    }

    function post($data) {
        if (isset($_POST[$data])) {
            $data = $_POST[$data];
            $data = trim(htmlentities($data));
            $this->data = $data;
        }
    }

    function post_inc($data) {
        if (isset($_POST[$data])) {
            $data = $_POST[$data];
            $data = preg_replace('/[^0-9]/','',$data);
            $this->data = $data;
        }
    }
    
    function post_gson($data) {
        if (isset($_POST[$data])) {
            $json = $_POST[$data];
            $json = json_decode($json);
            $this->data = $data;
        }
    }
    
    function string($data) {
        $data = trim(htmlentities($data));
        $this->data = $data;
    }

    function result() {
        return $this->data;
    }

}

I refer to him like this
$data->post_gson('data');
$json = $data->result();
print_r($json);

As a result, there is only "data" in the $json variable instead of the array I expected. How to be? I haven't been working with OOP for a long time. More specifically, one night

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Shiklgruber, 2015-06-23
@aaadddminnn

and got it all figured out. There I got confused in the variables.
was written

function post_gson($data) {
        if (isset($_POST[$data])) {
            $json = $_POST[$data];
            $json = json_decode($json);
            $this->data = $data;
        }
    }

but it was necessary
function post_gson($data) {
        if (isset($_POST[$data])) {
            $json = $_POST[$data];
            $json = json_decode($json);
            $this->data = $json;
        }
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question