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
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:
- First one is just to sending and downloading data. For example SOAP class with caching.
- Second class is creating proper requests, and using 1. class.
- 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.