2016-06-08 78 views
1

我需要一個單例類的幫助。我正在創建一個wordpress插件,並且需要從服務器接收實時通知。爲此,我使用AJAX長輪詢,我的代碼看起來像這樣。單例類不只是一次在php中實例化

這是用於服務AJAX請求和LOG類是獨立的,並在項目來自許多不同的地方稱爲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); 
    }}?> 

這是JavaScript的檢索通知和一個在管理部分的一部分wordpress。

<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類Singleton模式實現單例類,所以不僅這個類的一個實例,但更多,當我嘗試檢索通知即。當我觸發某個動作時,通知不會存儲在同一個對象中。我用alert(resp);在cilent頁面顯示響應和響應看起來像這樣

{ 
"msg":false, 
"type":"something" 
} 

和log.php你可以看到,類型值是好的,所以它不是溝通的問題。任何人都可以幫助我嗎?

注:我必須使用JavaScript,因爲版本問題,所以不要問我爲什麼,我沒有使用JQuery

+1

我認爲你誤解了PHP的工作原理。單身人士在不同的請求中並不是唯一的。 – tkausl

+0

你是說爲每個請求實例化一個對象而不是在該類的外部? – djoleasterix

+0

他正在試圖說谷歌PHP的「持久性」,它會給你一些想法。根據你上面的代碼,除了你現在得到的迴應之外,沒有任何代碼可以檢索任何代碼。任何以前的連接都已經關閉,因爲已經給出了響應(再次根據給出的信息...你還沒有提到套​​接字/等 - 例如谷歌棘輪)。但不是一個不錯的努力和香草js +1! – David

回答

0

的單件模式是非常有用的,當我們需要確保我們只有一個實例一個Web應用程序中整個請求生命週期的類。

所以,你不能用你想要的方式做到這一點。

取而代之,將其用作基類/父類,並在需要時將其擴展到其他類。