how to make my own facebook class

2019-09-17 02:20发布

问题:

i want to make my own class to make my code useful and easy. i want to replace (for example)

$facebook->api('/me');

to

$myclass->me();

to post on wall for example i need to write

$facebook->api('/me/feed','post',$atch);

i want to make it

$myclass->msg($msg);

how can i make my own class using facebook class (and i using another DB class)

thanks for halp

回答1:

Option #1 - quick, easy, common practice for OOP

class myFacebook extends facebook {
  public function me() {
    return $this->api('/me');
  }
  public function msg($msg) {
    return $this->api('/me/feed','post',$msg);
  }
}

Option #2 (alternative) - easyier to maintance, won't collide with base class properties, easy to extend. It's like API for API :)

class myFacebook {
  public $api;
  public function __construct() {
    $this->api = new facebook(); // your base class
  }
  public function me() {
    return $this->api->api('/me');
  }
  public function msg($msg) {
    return $this->api->api('/me/feed','post',$msg);
  }
  public function api() {
    // more difficult to declare that function in #1 option
  }
}

2nd option is better when your class uses lot of keywords and may collide with base API class. Easyier to maintance, easier to extend.


I used to work with many API's (ebay,paypal,amazon,fb etc). I usually create 2-3 classes:

  1. First one is just to sending and downloading data. For example SOAP class with caching.
  2. Second class is creating proper requests, and using 1. class.
  3. The most simplified one (like yours) - just having easy, and quick shortcuts to request class (which is your base class)

I know using extend the is most common practice, but personaly I preffer option #2.