2011-02-17 78 views
0

我想集成Google Analytics和Marchex(呼叫跟蹤系統)Call Analytics API來提取具有轉化率和類似內容的自定義報告。我對面向對象編程的世界非常非常陌生,並且認爲我需要一些指導才能實現這一目標。在我繼續前進之前,我想對構造函數進行一些批評。我是做對了還是什麼?將2個API集成到1個php類(OOP新手)

<?php 
require_once("gapi.class.php"); 
require_once("xmlrpc.inc"); 
class garchex 
{ 
    private $marchex_credentials = array(), 
      $ga_credentials = array(); 
    private $marchex_account, $ga_account; 
    public function __construct($marchex_credentials,$ga_credentials) { 
     assert(is_array($marchex_credentials)); 
     assert(is_array($ga_credentials)); 

     //setup marchex stuff 
     $this->marchex_credentials = $marchex_credentials; 
     $this->marchex_account = new xmlrpc_client("/api/xmlrpc/1", "api.voicestar.com"); 
     $this->marchex_account->SetCredentials($marchex_credentials[0],$marchex_credentials[1]); 

     //google analytics stuff 
     $this->ga_credentials = $ga_credentials; 
     $this->ga_account = new gapi($ga_credentials[0],$ga_credentials[1]); 
    } // __construct 
} // class garchex 
?> 
+0

你的意圖是同時使用一個或其他,或兩者? – meagar 2011-02-17 22:23:18

+0

這不是真正的問題。也許發佈在程序員或代碼審查。 – Phil 2011-02-17 22:24:29

回答

1

您使用 - > $語法分配了一些非靜態變量,這是不正確的。這裏有一個固定的建議:

<?php 

// Better use require_once, in case someone loads the file again 
require_once("gapi.class.php"); 
require_once("xmlrpc.inc"); 

class garchex 
{ 
    private 
     $marchex_credentials = array(), 
     $ga_credentials  = array() 
     ; 

    public function __construct($marchex_credentials, $ga_credentials) { 

     // check, if values are fine 
     assert(is_array($marchex_credentials)); 
     assert(is_array($ga_credentials)); 

     //setup marchex stuff 
     $this->marchex_credentials = $marchex_credentials; 

     $marchex_account = new xmlrpc_client("/api/xmlrpc/1", "api.voicestar.com"); 
     $marchex_account->SetCredentials($marchex_credentials[0],$marchex_credentials[1]); 

     // google analytics stuff 
     // No $ to access non-static ivars 
     $this->ga_credentials = $ga_credentials; 

     // black hole: local variable assigned. 
     // You won't be able to access this from outside this method. 
     $ga_account = new gapi($ga_credentials[0],$ga_credentials[1]); 

    } // __construct 
} // class garchex 

// better drop the closing PHP tag