变量似乎在定义时未初始化

时间:2013-03-18 17:17:59

标签: php

我正在尝试对网站进行有组织的设置,但我遇到了一个问题,在类文件上,我使用了$website['website_name'] = 'Delusion Servers';但是当我将变量放在一个函数中时,它说它没有初始化。

<?php

/*
 * Delusion Servers
 * Website access at (delusionservers.com).
 * Developed, designed and managed by Lewes B.
 * PAGE: WEBSITE.CLASS.PHP
 */

$website['website_name'] = 'Delusion Servers';

class website {

    function killPage($content){
        die("

            <h1>" . $website['website_name'] ."encountered an error</h1>

            <br /><br />" . $content . "

            ");
    }
}

$website = new Website;
?>

怎么了?

3 个答案:

答案 0 :(得分:3)

你在课堂上的作用是在不同的范围内。

试试这个:

class website {

    function killPage($content){
        die("

            <h1>" . $this->website_name ."encountered an error</h1>

            <br /><br />" . $content . "

            ");
    }
}

$website = new Website;
$website->website_name = 'Delusion Servers';

答案 1 :(得分:1)

class website {

   public $website = array();

    public function __construct() {
        $this->website['website_name'] = 'Delusion Servers';
    }

    function killPage($content){
        die("

            <h1>" . $this->website['website_name'] ."encountered an error</h1>

            <br /><br />" . $content . "

            ");
    }
}

答案 2 :(得分:-1)

解决方案1:类常量

class Settings {
    const WEBSITE_NAME = 'Delusion Servers';
}

class website {

    function killPage($content){
        die("<h1>" . Settings::WEBSITE_NAME ." encountered an error</h1>
             <br /><br />" . $content);
    }
}

解决方案2:全局常量

define("WEBSITE_NAME","Delusion Servers");
...
class website {

    function killPage($content){
        die("<h1>" . WEBSITE_NAME ." encountered an error</h1>
             <br /><br />" . $content);
    }
}

解决方案3:类属性

class website {
    private $websiteName = "Delusion Servers";

    function killPage($content){
        die("<h1>" . $this->websiteName ." encountered an error</h1>
             <br /><br />" . $content);
    }
}