【发布时间】:2011-04-25 21:44:49
【问题描述】:
我有这个 API,需要我有一个特定的数组键才能发送。 由于该数组需要在所有类方法上使用,我正在考虑将其作为类属性。
abstract class something {
protected $_conexion;
protected $_myArray = array();
}
稍后,关于这个类的方法,我将使用:
$this->_myArray["action"] = "somestring";
(其中“action”是需要发送到此 API 的密钥);
这样好吗?我没有在我眼前看到足够的 OOP,这就是我问这个的原因。
根据要求,这里是有关 API 的更多信息:
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);
$data = utf8_encode($data);
$xml = new SimpleXMLElement($data);
if($xml->result == "success")
{
return $xml;
}
else
{
return $xml->message;
}
}
}
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 getPaymentMethods()
{
//this is the necessary field that needs to be send. Containing the action that the API should perform.
$this->_postFields["action"] = "dosomething";
//not sure what to code here;
if($apiReply->result == "success")
{
//works the returned XML
foreach ($apiReply->paymentmethods->paymentmethod as $method)
{
$method['module'][] = $method->module;
$method['nome'][] = $method->displayname;
}
return $method;
}
}
}
非常感谢, 内存
【问题讨论】:
-
嗯,我不知道你为什么需要这个数组键存在,但可以肯定的是,对我来说看起来非常好
-
没有足够的信息来说明
$_myArray属性是否正确。存储在其中的数据是否需要在something方法调用中持续存在?告诉我们有关您正在使用的 API 的更多信息。 -
@outis - 不确定持久部分。该属性用于所有方法,但该属性的值,至少到现在为止,似乎没有从一个方法传递(相同的值)到另一个方法。 (这是你要求的吗?)
-
我认为这回答了我的问题。如果在一个方法调用中存储在数组中的任何数据在另一个调用中使用,则数据将需要持久化。另一方面,如果您可以在每个方法结束时清空数组并且程序仍然可以正确执行,则数据不需要持久化。你是说后者成立吗?
-
@outis - 它不会持续存在。 :( 所以我做得不好? - 我会告诉更多关于 API 的信息
标签: php arrays class properties