note 83727 added to internals2.objects

From: Date: Mon, 09 Jun 2008 15:39:36 +0000
Subject: note 83727 added to internals2.objects
Groups: php.notes 
Request: Send a blank email to php-notes+get-140274@lists.php.net to get a copy of this message
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. ---- Server IP: 69.147.83.197 Probable Submitter: 143.111.22.24 ---- Manual Page -- http://www.php.net/manual/en/internals2.objects.php Edit -- https://master.php.net/note/edit/83727 Del: integrated -- https://master.php.net/note/delete/83727/integrated Del: useless -- https://master.php.net/note/delete/83727/useless Del: bad code -- https://master.php.net/note/delete/83727/bad+code Del: spam -- https://master.php.net/note/delete/83727/spam Del: non-english -- https://master.php.net/note/delete/83727/non-english Del: in docs -- https://master.php.net/note/delete/83727/in+docs Del: other reasons-- https://master.php.net/note/delete/83727 Reject -- https://master.php.net/note/reject/83727 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#140274) next »