0

I'm gonna make this too complicated, just going to break it down to the main parts.

I have a form that changes the boolean of a variable when the form gets submitted, however it gets called by a function, the function has to change the variable.

class updates
{
    var $yesno = false;

    function updateBool()
    {
        $this->yesno = true;
    }
}

So when the form gets submitted, it will call $up->updateBool() to change the boolean to true. When I do var_dump($up->yesno), it says false when it should be true. If I do this:

class updates
{
    var $yesno = false;

    function updateBool()
    {
        $this->yesno = true;
        var_dump($this->yesno); // <-- outputs true
    }
}

So how come I cannot get the variable to print out true in a seperate script?

EDIT:

$sql = "SELECT boolean
        FROM config
        WHERE boolean = 'true'";

$result = mysql_query($sql);

if(mysql_num_rows($result) > 0)
{
    $up->updateBool();
}
else
{
    header("Location: index.php?d=none");
}

This is part of the code where it gets called. I can confirm there are more than 1 record in the SQL statement.

3
  • 1
    Please show the exact code you are using when it returns false Commented Mar 16, 2011 at 21:46
  • And when exactly are you doing the var dump?` Commented Mar 16, 2011 at 21:55
  • And where in that new code do you do the var_dump? Commented Mar 16, 2011 at 21:55

2 Answers 2

1

So when the form gets submitted, it will call $up->updateBool() to change the boolean to true

You seem to be switching to a new page, where $up will be a new object. Objects do not persist across requests. PHP "loses its memory" when you call a new page, and all variables are started from scratch.

To persist values across page requests, you would need to use something like sessions.

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

7 Comments

Hmmm. Your answer seems reasonable, I always thought objects can be passed around in PHP after moving from one page to another. I shall accept your answer due to a logical answer.
@lolwut sessions are usually the way, although you shouldn't put too much data into a session. For a fancy advanced fast way to persist objects, see stackoverflow.com/questions/2940610/…
In order to pass objects around you need to start a session and then assign the object to a session variable. Otherwise, all objects only exist for the lifetime of the page and recreated each time the page is requested.
What if the variable/object was meant to be private so it wouldn't be logical to set them into a session that can be tampered with.
@lolwut that is true, yeah. Nothing should be stored in cookies but chocolate cream. Delicious, delicious chocolate cream.... (drool)
|
0
class updates
{
    public $yesno;
    function __construct(){
        $this->yesno = false;
    }

    function updateBool()
    {
         $this->yesno = true;
    }
}

Comments

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.