Tuesday, 28 August 2018

The PHP class does not save the variable

I have a simple API class that talks with its web-based counterpart. All works fine, just one variable denies to be saved.

I have a variable called $scope, initialised at the top of the class:
class Api {

    private $scope;

    public function set_scope( $s ) {
        $this->scope = serialize($s);
        return true;
    }

    private function get_scope() {
        return unserialize($this->scope);
    }

}

Next I'm getting the scope via API from the web script as JSON, I json_decode it as an array (second parameter as true) and in that form, I pass it through set_scope() function.
I am 10000000% sure API returns JSON, when var_dump'ed it returns me a proper array data.
For some reason though, that data is not saved into the $scope variable.
Any ideas?
Edit #1:
I call function get_scope within the class, in another function. I was trying to set that variable directly, so just using $this->scope=$scope, without success though. I use the same structured functions to save/get other variables, and all of them are working, except this one.
Other function looks like that:
public function get_modules() {

    $av_modules = $this->available_modules;
    $scope = $this->get_scope();

    var_dump($scope);

    foreach($scope as $mod) {
        $av_modules[$mod] = true;
    }

    return $av_modules;
}


The get_scope method is private (thus cannot be called from outside itself).
Make the get_scope method public and the value will be returned.

0 comments:

Post a Comment