views:

33

answers:

1

I have a flash application which uses a single php file to retrieve records from a database (using the Zend framework). When the application first begins, I make a call to the php to set a class variable, so that all future requests to the database will use this variable to select records based on its value. So here is how the class begins:

class MyClass
{   

    private $animal = "";

    public function setAnimal($anim) {
         $this->animal = $anim;
         echo($this->animal); //this correctly prints the variable I passed in
    }

Later, based on user input, I make a call to a different method in this class, but it's as if the class variable $animal has been forgotten, because it no longer has a value on any subsequent accessing of the class:

public function getAnimals()
    {
        echo('getAnimals: ');
        echo($this->animal); //this prints nothing - as if it doesn't know what "animal" is

        $result = mysql_query("SELECT * FROM animals WHERE animal='$this->animal'"); //and therefore this query doesn't work
        $t = array();

        while($row = mysql_fetch_assoc($result))
        {
            array_push($t, $row);
        }

        return $t;
    }

So my question is, how can I get a PHP class variable to persist so that I can set it once, and I can access it anytime during the life of an application?

+2  A: 

I could be mis-interpreting your question, but it sounds like you first make a call to a PHP script from your Flash, and later you are making a second call to the PHP script from the Flash and expecting a certain variable to be set?

If this is the case, then it is also the problem. PHP is stateless. Every time you access a PHP script (ie, request the URL), the PHP environment is re-created from scratch. As soon as the request is done and the PHP script is finished executing, the environment is destroyed (ie. the web server thread shuts down, and the PHP environment is lost). Anything you set or do in your first request won't exist on your second request.

If you want information to persist, you can use sessions or cookies. Since you're using Flash, sessions is probably the best way to go. The first time you call your script, generate a session token and pass it back to the flash with your response. On all subsequent calls, your Flash should provide the session token, and you can store/fetch any state variables you need from $_SESSION.

zombat
Yeah, that's what I was afraid of. Looks like I have to do some research on sessions and flash. Thanks.
sol