PHP 致命错误:无法重新分配 $this

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2699587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:24:06  来源:igfitidea点击:

PHP Fatal error: Cannot re-assign $this

php

提问by Arlen Beiler

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\Apache Software Foundation\Apache2.2\htdocs\abp\fol\test.php on line 27, referer: http://abp.bhc.com/fol/

Here is the code which produces the error:

这是产生错误的代码:

<?php
$voiceboxes = array(
    '141133'    => array(
        'title' => 'Title',
        '1'     => array(
            'Title' => 'Title2',
            'Link'  => 'http://...',
        ),
        '12'    => array(
            'Title' => 'Title3',
            'Link'  => 'http://...',
        )
    ),
    '1070453'   => array(
        'title' => 'Title4',
        '1'     => array(
            'Title' => 'Title5',
            'Link'  => 'http://...',
        )
    )
);
$last = 0;
//$this = 0;
echo "<ol>\n";
foreach ($voiceboxes as $key => $value) {
    $last = 0;
    $this = null; //Error is thrown here, Line 27
    //$voiceboxes[$key]['title']
    echo "<ol>\n";
    foreach ($value as $key2 => $value2) {
        if ($key2 == 'title') {
            echo "<li>$value2</li>\n";
        } else {
            $this = (int) $key2;
            if ($this == $last + 1) {
                echo '<li>';
            } else { '<li value="' . $key2 . '">';}
            $last = $key2;
            echo $voiceboxes[$key][$key2]['Title'] . "<br/>" . $voiceboxes[$key][$key2]['Link'] . '</li>' . "\n";
        }
    }
    echo "</ol>\n";
}

回答by Oddthinking

$thisis a predefined variable in PHP.

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

这是 PHP 手册中的参考资料:Classes and Objects: The Basics。它描述了在一个方法中,$this 如何指向正在被操作的“这个对象”。不过,它仍然保留在方法之外。

Change the identifier to another word.

将标识符更改为另一个词。

回答by meagar

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

$this是php中的一个特殊变量。如果此代码发生在类内部,$this则是对调用方法的对象的引用。您不能$this在类内部分配新值。PHP 的一个限制是,您也不能分配给$this在类之外命名的变量,否则这样做是有效的。

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

我相信这在 PHP4 中是有效的,但是从 PHP5 开始,您必须选择一个新的变量名称。

回答by phimuemue

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

我不是真正的 PHP 专家,但我认为$this是指当前对象,所以如果您设置thisnull,则尝试将当前对象设置为空,这将无法正常工作。

回答by Parameshwaran Manickkam

You can reassign $thisvalue by variable variable

您可以$this通过变量变量重新分配值

$name = 'this';
$$name = 'stack';
echo $this;
// this will result stack