Re: php 5.2.x - Fatal error: Cannot re-assign $this in ...
| From: | Shawn McKenzie | Date: | Tue, 25 Aug 2009 20:09:02 +0000 |
| Subject: | Re: php 5.2.x - Fatal error: Cannot re-assign $this in ... | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-297215@lists.php.net to get a copy of this message | ||
John Stoffel wrote:
> Hi,
>
> I'm looking for some help with PHP5.2.x hacking on a web site to
> cleanup how the user submitted search form is handled. Basically I
> want to make it simple for the end user to a) refine their query, b)
> not have to answer popups if they hit the reload button, and c) so I
> can learn more PHP. I found this nice reference:
>
> http://us2.php.net/manual/en/language.variables.external.php
>
> which has two sections by "wayne" and "Jonas Lindel" talking about how
> you can take the results of a PHP POST request, stuff the vars into
> your session, and redirect back to the same page without the POST vars
> set, then complete the query.
>
> The idea is so that when people hit the "back" or "reload" buttons,
> they don't keep getting asked whether they want to resubmit their
> data.
>
> What I'm trying is not strictly needed, but it would be a nice cleanup
> of the interface of the search forms for this site.
>
> I've been googling, reading php5.x docs and trying to understand what
> needs to be done here. I think I'm just missing how to re-assign
> $this properly, so that when the redirect happens, the proper env is set.
>
> Here's the snippet I'm trying to use, which bombs out with:
>
> Fatal error: Cannot re-assign $this in .... on line 45
>
> And the code I'm using is:
>
> <?
>
> // We want to put the search query into a session so we can restore it
> // easily when users goto look at full_holdings.php and then return to
> // the overall display page. We'll need to do a session_destroy in
> // index.php I think. See the tutorial(s) at
> // http://www.phpf1.com/tutorial/php-sessions.html
> // http://us3.php.net/manual/en/book.session.php
>
> session_start();
>
> // Now see Jonas Lindel's & Wayne's code at
> // http://us2.php.net/manual/en/language.variables.external.php
> for
> // this *nice* trick.
>
> //is there POST or GET data we should look at
> if($_POST or ($_GET and (!isset($_SESSION['skipnextget'])))) {
>
> //is there GET data?
> if($_GET) {
> $newgetarr = array();
> foreach($_GET as $key => $value) {
> if(is_array($value)) {
> //if it's an array, convert it to comma separated
> $newvalue = implode(',',$value);
> } else {
> $newvalue = $value;
> }
> $newgetarr[] = $key.'='.$newvalue;
> }
> $newget = implode('&',$newgetarr);
> $_SESSION['skipnextget'] = 1;
> }
> foreach($_POST as $key => $value) {
> $this[$key] = $value;
> }
> $_SESSION['postvars'] = serialize($this); //put POST in the session
> header("Location: http://"
> .$_SERVER['HTTP_HOST']
> . $_SERVER['PHP_SELF'] . '?\
> ' . $newget);
> //reload page without POST data and shorter GET data
>
> exit();
> } else { //after reloading we'll come right to this spot
> session_unregister('skipnextget');
> if(isset($_SESSION['postvars'])) {
> //load the POST variables from the session
>
> // Error on this line below!
> $this = unserialize($_SESSION['postvars']);
> session_unregister('postvars'); //delete the session stuff we
> //don't need anymore.
> return $this;
> }
> }
>
> // rest of my code goes here...
> _______________________________________________
>
> Thanks,
> John
> john@stoffel.org
I'm not sure if it is technically reserved, but $this is used in a class
in order for an object to refer to itself. AFAIK you can't use it
anywhere other than that. Just change all $this to $vars or something else.
--
Thanks!
-Shawn
http://www.spidean.com