다음 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 -->
; 사람이 더 나은 방법을 생각할 수있는 경우
<?php component_customer_login(); ?>
가 나는 또한 출력 등되기 전에 형태를 확인하기 위해 "고객"클래스의 메서드를 렌더링 미리 호출 할 필요가
알려 만에 주시기 바랍니다 첫 번째 인스턴스에서 왜 위에서 언급 한 "치명적인 오류"가 발생하는지 알고 싶습니다.
'Customer :: __ construct()'와 'Customer :: init()'는 어떻게 생겼습니까? – dnagirl
@dnagirl - 질문과 관련이 없습니다. init() 함수가 없습니다. –
함수가 호출되지 않으면 함수에서 치명적인 오류를 얻을 수 없습니다. '고객 '이 다른 클래스를 확장한다면 당신은 말하지 않았습니다. 만약 그렇다면 아마도'init()'함수가있을 것이다. – dnagirl