为我的应用程序创建助手

发布于 2024-10-07 15:51:27 字数 2541 浏览 0 评论 0原文

我一直在为 Facebook PHP API 创建一个帮助器类,以避免重复使用大量代码。助手可以工作,但唯一的问题是它非常慢..我也找到了原因!当我初始化类时,构造函数被调用两次!我签入了我的代码,而使用此类的其他元素仅调用它一次(这是类本身内部的内容)您能帮我找出问题所在吗?谢谢!

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; }
    }

}

I have been creating a helper class for the Facebook PHP API in order to avoid reusing a lot of code. The helper works but the only problem is that its very slow.. and I also figured out why! when I initialize the class, the constructor is called twice! I checked in my code and the other elements which use this class only call it once (It's something inside the class itself) Could you please help me figure out what the problems could be?? Thanks!

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; }
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一身软味 2024-10-14 15:51:27

您必须调用 FbHelper 类两次,因为您的 doLog 函数位于构造函数中,因此重复位于应用程序中较高的位置,而不是此类本身。

You must be calling the FbHelper class twice as your doLog function is in the constructor, therefore the repetition is somewhere higher up in your application and not in this class itself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文