0

我正在尝试对网站进行有组织的设置,但我遇到了一个问题,在 Class 文件上,$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;
?>

怎么了?

4

3 回答 3

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';
于 2013-03-18T17:18:38.590 回答
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 . "

            ");
    }
}
于 2013-03-18T17:19:37.167 回答
-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);
    }
}
于 2013-03-18T17:21:52.203 回答