Cannot re-assign $this?

孤人 提交于 2019-12-06 00:18:20

$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.

You can reassign $this value by variable variable

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

// this will result stack

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.

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

$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.

You can use refrences trick:

$thisRef = &$this;
$thisRef = new $db($serv, $user, $pass, $dbName);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!