note 87311 added to language.references.arent

From: Date: Sat, 29 Nov 2008 15:52:17 +0000
Subject: note 87311 added to language.references.arent
Groups: php.notes 
Request: Send a blank email to php-notes+get-147419@lists.php.net to get a copy of this message
I have $scriptVar and $_SESSION['sessionVar'] and I want to bind them together using a custom SessionManager class. I want to selectively prefer the content of either var upon the situation - to be able to "link" either $scriptVar to $_SESSION['sessionVar'] (thus override the "content" of $scriptVar with the content of $_SESSION['sessionVar']) or to link $_SESSION['sessionVar'] to $scriptVar (vice versa). But as somebody mentioned here already, PHP's references aren't really references, so the simplistic way to do this is impossible to use. I came up with the following: <code> ... class def and other method declarations ... public static function &linkVar($varName, &$var = null, $overrideSessionValue = false) { if ($overrideSessionValue === true) { $_SESSION[$varName] =& $var; } else { return $_SESSION[$varName]; } } ... class def and other method declarations continue ... </code> Now, when I want to override the Session value, I simply call the function with three params, first being the session var name, second being a reference to the $scriptVar and the third a "true" telling the function to link $_SESSION[$varName] to $var, which links to $scriptVar and therefore to link $_SESSION[$varName] to $scriptVar. When I want to link it the other way, to override $scriptVar's value with $_SESSION[$varName] and link them together, I call the function with only the $varName argument and assign it's output to $scriptVar as a reference. Tested, working... at least under PHP 5.3. ---- Server IP: 147.32.160.17 Probable Submitter: 62.240.165.30 ---- Manual Page -- http://www.php.net/manual/en/language.references.arent.php Edit -- https://master.php.net/note/edit/87311 Del: integrated -- https://master.php.net/note/delete/87311/integrated Del: useless -- https://master.php.net/note/delete/87311/useless Del: bad code -- https://master.php.net/note/delete/87311/bad+code Del: spam -- https://master.php.net/note/delete/87311/spam Del: non-english -- https://master.php.net/note/delete/87311/non-english Del: in docs -- https://master.php.net/note/delete/87311/in+docs Del: other reasons-- https://master.php.net/note/delete/87311 Reject -- https://master.php.net/note/reject/87311 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#147419) next »