-3

我在一个名为 website 的类中有两个函数。这两个函数是 checkStatus 和 killPage。

函数 killPage 是关于使用漂亮的简单样式而不是完整的单词文本。函数 checkStatus 应该在代码中使用 killPage ,但它不会让我使用它。

继承人的代码:

class website 
{
    function killPage($content)
    {
        die("

            <h1>" . Settings::WEBSITE_NAME ." encountered an error</h1>

            " . $content . "

            ");
    }

    function checkStatus(){
        if(Settings::STATUS == 'M')
        {

            $website->killPage('We are in maintence');
        }
        if(Settings::STATUS == 'O')
        {
        }
        if(Settings::STATUS == 'C')
        {
            $website->killPage('We are closed');
        }
    }
}

$website = new Website;

我得到的错误:

未定义的变量:网站 && 调用非对象上的成员函数 killPage()

4

3 回答 3

2

$this指类的当前实例,而不是$classname

于 2013-03-22T20:26:32.163 回答
1

更改$website$this

class website 
{
    function killPage($content)
    {
        die("

            <h1>" . Settings::WEBSITE_NAME ." encountered an error</h1>

            " . $content . "

            ");
    }

    function checkStatus(){
        if(Settings::STATUS == 'M')
        {

            $this->killPage('We are in maintence');
        }
        if(Settings::STATUS == 'O')
        {
        }
        if(Settings::STATUS == 'C')
        {
            $this->killPage('We are closed');
        }
    }
}
于 2013-03-22T20:26:51.757 回答
0

问题在这里:

function checkStatus(){
    if(Settings::STATUS == 'M')
    {
        Settings::STATUS == 'M';
        $website->killPage('We are in maintence');
    }

您正在取消引用$website尚未纳入范围的内容。

在这种情况下,$website是一个全局变量,你需要把它带入作用域:

function checkStatus() {
    global $website;

    if(Settings::STATUS == 'M')

编辑或其他人指出,作为checkStatus成员函数,您应该使用$this.

于 2013-03-22T20:26:26.030 回答