如何使我自己的facebook类 [英] how to make my own facebook class
问题描述
我想让我自己的类,使我的代码有用和容易。
我要替换(例如)
i want to make my own class to make my code useful and easy. i want to replace (for example)
$facebook->api('/me');
到
$myclass->me();
在墙上张贴例如我需要写
to post on wall for example i need to write
$facebook->api('/me/feed','post',$atch);
我想要
$myclass->msg($msg);
如何使用facebook类(和i使用另一个DB类)创建自己的类
how can i make my own class using facebook class (and i using another DB class)
感谢halp
推荐答案
选项1 - 快速,简单,常见的OOP操作
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);
}
}
选项#强> - 易于维护,不会与基类属性碰撞,易于扩展。它就像API的API:)
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
}
}
第二个选项更好,当你的类使用了很多关键字,可能会与base API类冲突。
2nd option is better when your class uses lot of keywords and may collide with base API class. Easyier to maintance, easier to extend.
我常常使用许多API(ebay,paypal,amazon ,fb等)。
我通常创建2-3个类:
I used to work with many API's (ebay,paypal,amazon,fb etc). I usually create 2-3 classes:
- 第一个只是发送和下载数据。例如具有缓存的SOAP类。
- 第二类创建正确的请求,并使用1.类。
- - 只需要简单快捷的请求类(这是你的基类)
我知道使用 extend
这是最常见的做法,但personaly我preffer选项#2。
I know using extend
the is most common practice, but personaly I preffer option #2.
这篇关于如何使我自己的facebook类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!