how to make my own facebook class

荒凉一梦 提交于 2020-01-06 04:36:18

问题


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.



来源:https://stackoverflow.com/questions/12265864/how-to-make-my-own-facebook-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!