0

I'm new to PHP and have run into an issue with scope/updating variables. Here is what my code looks like:

class ExampleClass {
    private static $var = array("hi");

    public static function exampleFunction() {
        if(something) {
            self::$var[] = "hello";
        }
    } //I print out this change and it works, containing both hi and hello

    public static function getVar() {
        return self::$var;
    } //this does not return the updated array, only the original with hi

}

I've tried the solutions mentioned here (using global, using $GLOBALS, passing by reference): Changing a global variable from inside a function PHP but none of them have worked -- no matter what, it seems like the second function does not get the updated version of the class variable. To be clear, I am calling exampleFunction first and then calling the getter.

This: Trouble changing class variable seems like it could be my problem, but creating a setter and passing in the reference to var does nothing, nor does passing in a reference in any other function (I'm not sure I completely understand how it works).

I'm not sure what to do at this point, and any help would be appreciated.

EDIT: Here is how it's being called:

example.js:

$('#aButton').click(function() {
    getData();
    getVar();
});

function getData() {
$.ajax({
    url: '/exampleController/getData',
    success: function(response){
        console.log(response);
        alert("var success");
    },
    error: function(){
        alert('Error');
    }

});
}

function getVar() {
$.ajax({
    url: '/exampleController/getVar',
    success: function(response){
        console.log(response);
        alert("var success");
    },
    error: function(){
        alert('Error');
    }

});
}

in exampleController.php:

public static function getData() {
    $result = ExampleClass::exampleFunction();
    echo json_encode($result);
}

public static function getVar() {
    $var = exampleClass::getVar();
    echo json_encode($var);
}
Community
  • 1
  • 1
  • 1
    Your second link is to a question about C++, not PHP. – Barmar Jun 25 '15 at 19:27
  • It looks to me like the code should work. Are you sure the `something` condition is true? – Barmar Jun 25 '15 at 19:29
  • Your code works for me: http://ideone.com/Bl7Hf4. I change `if (something)` to `if (true)` – Barmar Jun 25 '15 at 19:32
  • `if (something)` is not valid PHP. Specifically, the word `something` in that conditional should be throwing an error since it's not a valid expression. – Major Productions Jun 25 '15 at 19:40
  • The `something` is true, I am able to print out the changes outside of the if statement. I am calling it in my javascript using ajax, which then gets sent to my controller, which looks like this: `$var = exampleClass::getVar(); echo json_encode($var);` – neckofpearls Jun 25 '15 at 19:41
  • Please provide a complete executable example demonstrating your problem. We're seeing your class declaration, but not when or how it's being called. – deceze Jun 25 '15 at 19:42
  • The `something` is actually a condition that evaluates to true, sorry that it was unclear. I've also added the code that calls the functions. – neckofpearls Jun 25 '15 at 19:52
  • So those functions are called in separate HTTP requests?! Yeah, that won't work. **PHP doesn't save any data between requests!** – deceze Jun 25 '15 at 20:02
  • Oh wow, I had no idea, thanks deceze! Is there a clean way for me to send several variables/arrays back to my javascript in one request? EDIT: Nevermind, this seems to be what I'm looking for http://stackoverflow.com/questions/22821138/using-ajax-to-call-php-and-return-multiple-variables. Thanks everyone for their help! – neckofpearls Jun 25 '15 at 20:08

1 Answers1

0

trying to get class variable which is updated from another function

Your code works, you just have to call the exampleFunction() first and then call getVar() and reassign the returned array. And try to fix the if(something) condition - it's not an PHP Error, but a Notice: Use of undefined constant something.

<?php

define('SOMETHING', true);    

class ExampleClass {
    private static $var = array("hi");

    public static function exampleFunction() {
        if(SOMETHING) {
            self::$var[] = "hello";
        }
    } //I print out this change and it works, containing both hi and hello

    public static function getVar() {
        return self::$var;
    } //this does not return the updated array, only the original with hi

}

// add hello to the array ExampleClass::$var
ExampleClass::exampleFunction();

// get $var
$var = ExampleClass::getVar();

var_dump($var);

Result:

array(2) {
  [0]=>
  string(2) "hi"
  [1]=>
  string(5) "hello"
}

Demo: https://ideone.com/4SiRnl

Jens A. Koch
  • 39,862
  • 13
  • 113
  • 141