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.

MacMac
  • 34,294
  • 55
  • 151
  • 222

2 Answers2

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.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • 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. – MacMac Mar 16 '11 at 22:00
  • @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 http://stackoverflow.com/questions/2940610/how-to-persist-objects-between-requests-in-php – Pekka Mar 16 '11 at 22:01
  • 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. – Steve Massing Mar 16 '11 at 22:02
  • 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. – MacMac Mar 16 '11 at 22:06
  • @lolwut who could tamper with a session? It's true you shouldn't store credit card data in one because an attacker might get hold of the temporary session files on the server under certain circumstances, but that is a deep system-level security problem. For most intents and purposes, sessions are safe – Pekka Mar 16 '11 at 22:08
  • Oh, my bad. I was thinking about those yummy cookies that can be tampered easily that is certainly a security problem when storing stuff inside cookies. – MacMac Mar 16 '11 at 22:15
  • 2
    @lolwut *that* is true, yeah. Nothing should be stored in cookies but chocolate cream. Delicious, delicious chocolate cream.... (drool) – Pekka Mar 16 '11 at 22:20
0
class updates
{
    public $yesno;
    function __construct(){
        $this->yesno = false;
    }

    function updateBool()
    {
         $this->yesno = true;
    }
}
Meberem
  • 937
  • 7
  • 18