싱글 톤 클래스에 대한 도움이 필요합니다. 나는 wordpress plugin을 만들고 서버로부터 실시간 통보가 필요하다. 이를 위해 AJAX long polling을 사용했고 내 코드는 다음과 같습니다. PHP에서 인스턴스화 된 싱글 톤 클래스
이
은if (isset($_GET['log']) && $_GET['log'] == 'true')
{
$response = array();
$response['msg'] = SI_log::get_instance()->get_message();
$response['type'] = 'something';
echo json_encode($response);
}
class SI_log{
private $log_messages = array();
private static $instance = NULL;
private $log_file;
public static function get_instance()
{
if (static::$instance === NULL) {
static::$instance = new static();
}
return static::$instance;
}
public function add_message($message, $type)
{
array_push($this -> log_messages, $message);
}
public function get_message()
{
return end($this->log_messages);
}}?>
이 검색 알림 자바 스크립트이며 관리자 섹션의 일부에 AJAX 요청을 봉사하고 싱글과 프로젝트에 많은 다른 장소에서 호출 LOG 클래스에 사용되는 PHP 코드입니다 워드 프레스.
<script type="text/javascript" charset="utf-8">
function waitForMsg(){
setTimeout(waitForMsg,5000);
document.getElementById("alerts").childNodes = new Array();
var request = new XMLHttpRequest();
request.open('GET', '<?php echo get_site_url() . '/wp-content/plugins/si/admin/c-si-log.php?log=true'?>', true);
request.onload = function() {
if (request.status >= 200 && request.status < 400) {
var resp = request.responseText;
alert(resp);
var json = eval('('+resp+ ')');
document.getElementById("alerts").innerHTML= json['type'] +"<hr>";
if (json['type'] == 'WARNING'){
var html_element = '<div class="alert-message warning" id = "alert_warning"><div class="box-icon"></div><p>'+json['msg']+'</p></div>';
}
if (json['type'] == 'INFO'){
var html_element = '<div class="alert-message info" id = "alert_info"><div class="box-icon"></div><p>'+json['msg']+'</p></div>';
}
if (json['type'] == 'ERROR'){
var html_element = '<div class="alert-message errorr" id = "alert_error"><div class="box-icon"></div><p>'+json['msg']+'</p></div>';
}
document.getElementById("alerts") . innerHTML= html_element;
}else{
alert('<?php echo get_site_url() . '/wp-content/plugins/si/admin/c-si-log.php?log=true' ?>');
}
};
request.onerror = function() {
// There was a connection error of some sort
alert("request isnt good");
};
request.send();
}
window.onload = function(){
if (document.readyState != 'loading'){
waitForMsg();
} else {
document.addEventListener('DOMContentLoaded', waitForMsg);
}
}
</script>
이
SI_log::get_instance()->add_message("action triggered", 'INFO');
내가 문제가 SI_log 클래스의 싱글 톤 패턴을 구현 가정 알림 입력을 다른 클래스에서 호출 싱글 톤 클래스는 방법, 그래서 그 클래스의뿐만 아니라 하나 개의 인스턴스가 존재하지만, 더 많은, 그리고 내가 알림을 검색하려고하면 즉. 내가 어떤 행동을 유발할 때, 알림은 같은 대상에 저장되지 않습니다. 나는 경보 (resp)를 사용했다; cilent 페이지에서 응답을 표시하고 응답이
{
"msg":false,
"type":"something"
}
처럼 보이는 log.php에 사용자가 입력 한 값이 좋은 것을 볼 수 있으므로 통신 문제가 아니에요. 누구든지 제발 도와 주실 래요?
참고 : 우리가해야 할 때 내가 싱글 톤 패턴이 유용 JQuery와
저는 PHP가 어떻게 작동하는지 오해하고 있다고 생각합니다. 싱글 톤은 서로 다른 요청에서 고유하지 않습니다. – tkausl
당신은 하나의 객체가 모든 요청에 대해 인스턴스화된다는 말을하고 있습니까? – djoleasterix
그가 Google php "persistance"를 말하려고하는 것은 몇 가지 아이디어를 줄 것입니다. 위의 코드를 기반으로 지금 얻고있는 응답을 제외하고는 아무 것도 검색 할 코드가 없습니다. 이전의 모든 연결은 응답이 주어 졌기 때문에 닫 혔습니다 (다시 주어진 정보를 기반으로 ... 예를 들어 소켓/etc - google ratchet에 대해서는 언급하지 않았습니다). 하지만 나쁜 노력과 바닐라 js +1! – David