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);
}
9
  • 1
    Your second link is to a question about C++, not PHP. Commented Jun 25, 2015 at 19:27
  • It looks to me like the code should work. Are you sure the something condition is true? Commented Jun 25, 2015 at 19:29
  • Your code works for me: ideone.com/Bl7Hf4. I change if (something) to if (true) Commented Jun 25, 2015 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. Commented Jun 25, 2015 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); Commented Jun 25, 2015 at 19:41

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

3 Comments

I changed nothing. Just showing how to call the functions and reassign the array.
If you didn't change anything, how does this solve the problem he's having?
If you just want to show that it works as written, put a link to an ideone.com example in a comment.

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.