PHP Fatal error: Cannot re-assign $this

前端 未结 4 1433
天涯浪人
天涯浪人 2020-12-11 04:10

I get this error from my script:

[Fri Apr 23 10:57:42 2010] [error] [client 10.0.0.1] PHP Fatal error:  Cannot re-assign $this in C:\\\\Program Files\\\\Apac         


        
相关标签:
4条回答
  • 2020-12-11 04:32

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

    0 讨论(0)
  • 2020-12-11 04:37

    I'm not really a PHP expert, but i think $this is referring to the current object, so if you set this to null, you try to set the current object to nothing, wich can not work.

    0 讨论(0)
  • 2020-12-11 04:39

    $this is a special variable in php. If this code is taking place inside a class, $this is a reference to the object the method is being invoked on. You cannot assign a new value to $this inside of a class. It is a limitation of PHP that you also cannot assign to a variable named $this outside of a class, where it would otherwise be valid to do so.

    I believe this was valid in PHP4, but as of PHP5 You'll have to choose a new variable name.

    0 讨论(0)
  • 2020-12-11 04:54

    You can reassign $this value by variable variable

    $name = 'this';
    $$name = 'stack';
    echo $this;
    // this will result stack
    
    0 讨论(0)
提交回复
热议问题