Cannot re-assign $this?
Asked Answered
F

5

4

I have a script on a server that had php version 4. Now it is changed to php5 and the script does not function any more. I get this error:

Fatal error: Cannot re-assign $this in URL database.php line 88

In the file is a class that has a function. Inside is the line 88:

$this = new $db( $serv, $user, $pass, $dbName );

What does the error mean and how can I change it?

Fiddler answered 8/10, 2013 at 8:24 Comment(1)
us1.php.net/manual/en/language.oop5.basic.php There is an explanation of $this here.Wirra
V
3

$this is a special "variable" that always refers to the object the current function is executing in. It only makes sense inside functions that belong to a class; however, you are not allowed to use it anywhere else, and you may never assign to it. The solution is simply to rename the variable.

Vincenz answered 8/10, 2013 at 8:25 Comment(2)
Well, I renamed it to dbo, but that only gives me a new error: Fatal error: Call to undefined function: doquery() in URL QuizOperations.php on line 524 Line 524 $this->db->doQuery( ..... ); There are several functions with similar lines. If I changes these this variables to dbo, I get the same error message.Fiddler
@Atha: Is this your own code or code you have obtained from somewhere else? Is it written for PHP 4 or 5? Anyways, the source of the problem is a bit hard to tell from just that line, and since it is a different kind of error, you're probably better off posting it as a new question.Vincenz
M
1

You can reassign $this value by variable variable

$name = 'this';
$$name = 'stack';
echo $this;

// this will result stack

Mascagni answered 23/11, 2016 at 6:53 Comment(0)
S
0

The class is attempting to re-define itself ($this in php is the same as this in C#) and it can't be done. You should change $this to something else and the error should be gone then.

Shorter answered 8/10, 2013 at 8:27 Comment(0)
A
0

$this is a reserved variable name and cannot manually be assigned a value.

Ardent answered 8/10, 2013 at 8:40 Comment(0)
T
0

$this is a predefined variable in PHP.

Here's the reference in the PHP manual: Classes and Objects: The Basics. It describes how, inside a method, $this points to "this object" that is being operated upon. It is still reserved outside a method, though.

Change the identifier to another word.

Thessa answered 24/5, 2016 at 10:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.