2013-03-18 98 views
0

我試圖做一個網站的組織設置時將未初始化的,但我已經遇到了一個問題,在類文件,我用$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; 
?> 

怎麼了?

回答

-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); 
    } 
} 
+0

嗯,爲什麼要這些? – Neal 2013-03-18 17:24:52

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'; 
+0

你能更解釋一下,因爲我完全糊塗了? – Lewes 2013-03-18 17:19:50

+1

@Lewes看到我的更新。 – Neal 2013-03-18 17:19:59

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

      "); 
    } 
} 
+0

方法來設置WEBSITE_NAME即__contruct()將是很好過 – Waygood 2013-03-18 17:22:05

+0

做...感謝您的建議.. – 2013-03-18 17:24:51

+0

'$網站=新的網站(「妄想服務器」);'會更好!附:注意__W__ ebsite class – Waygood 2013-03-18 17:27:30