2015-09-29 43 views
1

我是新手與類(更糟糕的命名空間等),所以我知道必須創建FacebookMyClass.php類主要類是(autoload.php,Facebook.php,FacebookApp.php等),那麼我的原型:Facebook的PHP SDK 5:擴展類

<?php 
// I guess 
namespace Facebook; 

// Do I must 'use' or extending other class(es)? 
// use ; 

class FacebookMyClass extends Facebook { 

    // Do I have to "re-declare" parent's vars? 
    private $my_private_var; 
    public $my_public_var; 
    private $access_token; 

    // The main thing, I think, is how class is constructed 

    // public function __construct(array $config = []) { 
    public function __construct($id, $secret) { 
    // This will work? Need parameters? 
     parent::__construct(); 
    } 

    public function accessParentProtectedVars($idfanpage) { 
    // How can I call/link to Facebook object/request/response? 
    $object = $this->get('/' . $idfanpage . '/albums?fields=name,id', $this->access_token); 
    } 

    private function getTokens($params = []) { 
    // code... 
    $this->access_token = '...'; 
    } 

} 
?> 

問題是:我該如何擴展Facebook \ Facebook類(es)?

+1

你爲什麼不使用FacebookResponses getBody()方法?它返回body屬性。 '$ body = $ datos-> getBody();' – lordthorzonus

+0

@lordthorzonus你讓我的一週!儘管它沒有回答我關於擴展類的問題,但它爲我解決了一個大問題,Facebook文檔沒有(我希望可以像CodeIgniter用戶指南)。 – quantme

+0

希望下面的答案給你你正在尋找的解釋。 lordthorzonus是正確的,但如果可能的話,總是更好地使用類的現有方法,只有在定製超出可用範圍時才需要擴展或實現。 – Sean

回答

2

命名空間表示一個類或一組類在給定項目中的居住位置。

擴展類意味着在類的範圍內繼承(獲取)其方法。

因此,作爲一個例子,記住,你可能需要調整這個:

<?php 
// Imports the class Facebook from the Facebook namespace eg /Facebook/Facebook.php 
use Facebook\Facebook; 

class MyFacebookExtension extends Facebook { 

    function __construct(array $config = []) { 
     // The base class requires an array parameter, so we accept that and forward it, and then call the parent constructor 
     parent::__construct($config); 
    } 

    // .. whatever methods you want to add .. You can also now access facebook() methods like $this->getclient() etc within this class. 
}