2010-12-10 13 views
0

多くのコードの再利用を避けるため、Facebook PHP APIのヘルパークラスを作成しています。ヘルパーは動作しますが、唯一の問題は、その非常に遅いことです。私はまた理由を理解しました!クラスを初期化すると、コンストラクタが2回呼び出されます。私は自分のコードをチェックし、このクラスを使用する他の要素は一度しか呼び出されません(クラス自体の中の何かです)。ありがとう!私のアプリケーションのヘルパーの作成

class FbHelper 
{ 
    private $_fb; 
    private $_user; 

    function __construct() 
    { 
     // Initalize Facebook API with keys 

     $this->_fb = new Facebook(array(
      'appId' => 'xxxxxxxxxxx', 
      'secret' => 'xxxxxxxxxxxxxxxxxxxxxx', 
      'cookie' => true, 
     )); 

     // set the _user variable 
     // 
     $this->doLog("Called Constructor"); 
     // 
     $this->_user = $this->UserSessionAuthorized(); 

     return $this; 
    } 

    function doLog($text) 
    { 
     // open log file <----- THIS GETS CALLED TWICE EVERY TIME I INITIALIZE THE CLASS!! 
     $filename = "form_ipn.log"; 
     $fh = fopen($filename, "a") or die("Could not open log file."); 
     fwrite($fh, date("d-m-Y, H:i")." - $text\n") or die("Could not write file!"); 
     fclose($fh); 
    } 


    function getUser() { return $this->_user; } 

    function getLoginUrl() { return $this->_fb->getLoginUrl(); } 
    function getLogoutUrl() { return $this->_fb->getLogoutUrl(); } 

    function UserSessionAuthorized() 
    { 
     // Checks if user is authorized, if is sends back user object 

     $user = null; 

     $session = $this->_fb->getSession(); 
     if (!$session) return false; 
     try { 
      $uid = $this->_fb->getUser(); 
      $user = $this->_fb->api('/me'); 
      if ($user) return $user; 
      else return false; 
      } 
     catch (FacebookApiException $e) { return false; } 
    } 

    private function _rebuildSelectedFriends($selected_friends) 
    { 
     // Creates a new array with less data, more useful and less malicious 

     $new = array(); 
     foreach ($selected_friends as $friend) 
     { 
      $f = array('id' => $friend['id'], 'name' => $friend['name']); 
      $new[] = $f; 
     } 

     return $new; 
    } 

    function GetThreeRandomFriends() 
    { 
     $friends = $this->_fb->api('/me/friends'); 
     $n = rand(1, count($friends['data']) - 3); 

     $selected_friends = array_slice($friends['data'], $n, 3); 
     return $this->_rebuildSelectedFriends($selected_friends); 
    } 

    function UserExists($user_id) 
    { 
     try { $this->_fb->api('/' . $user_id . '/'); return true; } 
     catch (Exception $e) { return false; } 
    } 

} 

答えて

1

あなたのdoLog関数として二回FbHelperクラスを呼び出すする必要がありますしたがって、繰り返しが、このクラス自体のどこかに高いアプリケーションでアップしないで、コンストラクタです。

+0

はい。私はそれも考えています。 FbHelperを呼び出す人を追跡する方法はありますか?ありがとう – PirosB3

+0

これのためにdebug_print_backtrace()を使うことができます。 – StasM

+0

ありがとうございました!問題が見つかりました。Codeigniterを使用していて、クラスを2回ロードしました!すべての助けてくれてありがとう! – PirosB3

関連する問題