MacMac
MacMac

Reputation: 35321

Update Class variable outside PHP Class

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.

Upvotes: 0

Views: 1316

Answers (2)

Pekka
Pekka

Reputation: 449475

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.

Upvotes: 1

Meberem
Meberem

Reputation: 947

class updates
{
    public $yesno;
    function __construct(){
        $this->yesno = false;
    }

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

Upvotes: 0

Related Questions