php设计问题
我有以下课程:php设计问题,php,class,Php,Class,我有以下课程: class Apiconnect { const URL = 'https://someurl.com/api.php'; const USERNAME = 'user'; const PASSWORD = 'pass'; /** * * @param <array> $postFields * @return SimpleXMLElement * @desc this connects but also sends and retrieves the
class Apiconnect {
const URL = 'https://someurl.com/api.php';
const USERNAME = 'user';
const PASSWORD = 'pass';
/**
*
* @param <array> $postFields
* @return SimpleXMLElement
* @desc this connects but also sends and retrieves the information returned in XML
*/
public function Apiconnect($postFields)
{
$postFields["username"] = self::USERNAME;
$postFields["password"] = md5(self::PASSWORD);
$postFields["responsetype"] = 'xml';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, self::URL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
$data = curl_exec($ch);
curl_close($ch);
$xml = new SimpleXMLElement($data);
if($xml->result == "success")
{
return $xml;
}
else
{
return $xml->message;
}
}
}
我需要在getStuff()方法上使用属性$\u连接,以便将“操作”发送到API中。但不确定如何才能做到这一点
有什么帮助吗?为什么不扩展对象而不是包含一些内容呢。APIConnect应该是抽象类,API应该是扩展它的东西
abstract class ApiConnect {
protected $_something;
protected $_something2;
protected $_curlResource;
function __construct($param1){
/* do curl setup here and save it to $this->_curlResource */
}
}
class ApiSomething extends ApiConnect {
protected $_paramForBase;
function __construct(){
super($this->_paramForBase);
echo "Var from base: ".parent::_something;
/* do more things with curl here, through parent::_curlResource */
}
function setParamsForCurlCall(){
/* add curl parameters with curl here, through parent::_curlResource */
}
function execCurl(){
/* do your final curl call here */
}
}
然后你可以用抽象类做任何你想做的事情,你可以用API做一些事情,可以拥有基类的所有控制逻辑。当然,如果要在多个类中扩展ApiConnect,您只需要这样做。首先,我认为
ApiConnect
应该是一个单例(您只需要一次身份验证),如果您想在其他类中将其用作资源,您应该将其重命名为类似ApiConnect
。扩展它也是一种选择,但这取决于您的其他类所做的事情(如果您选择扩展,您的其他类也应该是单例的)。是否抽象取决于它是否可以按原样使用
在
APIConnection
(我更喜欢这样命名)中,您应该有一个调用方法doPost
,或者类似的东西,可以向API发出请求(尽可能保持通用性)。在构造函数中,如果要在实例化时进行身份验证,请调用login API方法。当您需要调用其他API方法时,请直接通过刚刚创建的doPost
进行调用。您可以通过以下规则改进设计:
-不要在构造函数中执行实际工作(请参见APIConnect)-偏好组合
-在构造函数中注入依赖项
-有意义的类/接口名称
-单一责任 我会像这样重构:
abstract class ApiSomething
{
protected $_connection;
protected $_postFields = array();
/**
* @desc - Composition.
*/
public function __construct()
{
require_once("apiconnect.php");
$this->_connection = new Apiconnect($this->_postFields);
}
public function getStuff()
{
//this is the necessary field that needs to be send.
//Containing the action that the API should perform.
$this->_postFields["action"] = "dosomething";
...
}
}
interface APIConnection { // responsible for the connection
public function connect();
}
class FacebookConnection implements APIConnection {
public function __construct($url, $name, $pass, $params) {
// set only connection parameters here. NO WORK DONE, NO CONNECTING
}
public function connect() {
// curl stuff
}
}
class FacebookFarmer {
public function __construct(APIConnection $connection) {} // inject dependency, composition
public function harvest() {} // responsible for the actions having the connection
public function milkCows() {}
}
class FacebookFarmController { // translates what user requests to do to an action
public function __construct(FacebookFarmer $farmer) {} // injection again, composition
public function doAction($request) {
// eg $action = $request['action'];
// then $this->famer->$action() if exists
}
}
我不知道这是否是一个你可以使用的例子,因为APIThis和APIThat有点难以理解。如果我误解了,请澄清您的意图。@Rook-此API似乎只有在我这样做时才起作用。但是你有什么建议呢?嗯,https是目前使用的最好的传输方式。像这样使用md5的真正问题是,现在md5散列是您的密码,因此如果有人获得md5散列,那么他们可以立即登录。密码很有可能像这样存储在数据库中,然后可以使用SQL注入来获得即时访问,从而无法达到哈希密码的目的。@Rook谢谢Rook。解决办法会过去吗?我问这个问题,因为在API文档中,他们说:为了登录,就这样做。所以我做了,因为我在编程方面不是那么聪明。然而,如果在我这方面,我可以做得更好,为什么不呢你的解释似乎给了我一些线索,我意识到,然而,我还不能理解它们…:我不知道你在问什么,这似乎很明显。md5散列只是一个字符串,必须在某个地方进行比较。md5不是加密算法,而是一个消息摘要函数。在你这一方,除了给他们发电子邮件,告诉他们要求一个真正的解决方案之外,你真的什么都做不了:)可能这个抽象用法是我这一方的失误(老实说,我不需要抽象这个应用程序上的curl用法-这将是我唯一要使用的curl用法。连接/检索/发送不需要抽象发送方法或其他东西。即使在最远的将来,也不需要。因此,我相信抽象是一个潜在客户:(如果我们还需要连接其他API,那么该接口是否会有很好的用途?但是,在这种情况下,所有1个API都将在该应用程序的整个生命周期中都需要。这是一些用途的附加信息吗?我不确定实现扩展类的接口之间有什么区别,不用担心,我会阅读。我不知道关于注入及其与合成的关系,我有一个问题,在你的例子中,FarmController是如何使用连接的?它的使用方式与你的例子中的相同。因为我并不真正理解我只是假设它需要它。你可以将接口视为一种责任。在这种情况下,它说“我负责建立连接"。不同的类可以以不同的方式实现职责,而客户端类不关心如何实现。就我所知,抽象似乎是不符合条件的。关于我应该尽可能保持doPost的通用性这一事实,我不确定我是否可以做到这一点,因为API希望使用curl可以使用一些数组和r因此,我相信这正是他们需要的方式,也许。。。