类和对象的使用

add a note add a note

User Contributed Notes 1 note

up
-26
markem at airmail dot net
16 years ago
One of the things that I believe should be done in the "__construct" part of a class object is the definition of your variables which are going to be used outside of the class itself.  (Like the $_SESSION information or if you are going to be working with a database and you want to be able to monitor the incoming information and you always want it to go to the same variable.)  I came up with the following which I thought was kind of clever and I just wanted to pass it along so someone else doesn't have to re-invent the wheel.  Put this into your "__construct" function and set the $checkVar to be what is on your forms followed by what name you want the $_SESSION variable (or any variable for that matter) to be set it.  (We deal with small databases so the $_SESSION variable doesn't get all that large.)

    $checkVar['prevPage'] = 'PREVPAGE';
    $checkVar['newPWD'] = 'NEWPWD';
    $checkVar['confirmPWD'] = 'CONFIRMPWD';

    foreach( $checkVar as $key => $value ){
        if( isset($_POST[$key]) ){ $_SESSION[$value] = $_POST[$key]; }
            else if( isset($_GET[$key]) ){ $_SESSION[$value] = $_GET[$key]; }
            else if( isset($_SESSION[$value]) ){}   #   Do nothing
            else { $_SESSION[$value] = ""; }
        }

Here you can see I am keeping track of the previous page, the user's new password, and the confirmation of that password.  The FOREACH loop checks to see if the $_POST variable is set to the key and if so it sets the $_SESSION variable.  The thing is - it uses the associative array to keep track of which $_POST or $_GET entry to use.  Further, if the neither of those two are set it checks first to see if the $_SESSION variable was already set and then if not it at least sets it to a null string so it is defined.  This keeps us from having the error log messages about us using undefined variables too.

I hope everyone likes this and can use it.
To Top