我有以下PHP代碼;PHP靜態對象給出致命錯誤
<?php
component_customer_init();
component_customer_go();
function component_customer_init()
{
$customer = Customer::getInstance();
$customer->set(1);
}
function component_customer_go()
{
$customer = Customer::getInstance();
$customer->get();
}
class Customer
{
public $id;
static $class = false;
static function getInstance()
{
if(self::$class == false)
{
self::$class = new Customer;
}
else
{
return self::$class;
}
}
public function set($id)
{
$this->id = $id;
}
public function get()
{
print $this->id;
}
}
?>
我得到以下錯誤;
Fatal error: Call to a member function set() on a non-object in /.../classes/customer.php on line 9
有誰能告訴我爲什麼我得到這個錯誤?我知道這段代碼看起來很奇怪,但它基於我爲CMS編寫的組件系統。目標是能夠替換模板中的HTML標籤,例如:
<!-- component:customer-login -->
with;
<?php component_customer_login(); ?>
我還需要調用預渲染的「客戶」類的方法來驗證表單由輸出前等
如果有人能想到更好的辦法,請讓我知道,但是在首先,我想知道爲什麼我會收到上面提到的「致命錯誤」。
什麼是Customer :: __ construct()和Customer :: init()?看起來像什麼? – dnagirl 2010-05-05 13:50:41
@dnagirl - 它們與問題無關。沒有init()函數。 – 2010-05-05 13:54:18
如果函數未被調用,則不能在函數上發生致命錯誤。你還沒有說過'客戶'是否擴展了另一個班級。如果是這樣,也許'init()'函數就在那裏。 – dnagirl 2010-05-05 13:59:40