Ho questa API che mi richiede di avere una specifica chiave di array da inviare. Dato che quell'array deve essere usato su TUTTI i metodi di classe, stavo pensando di metterlo come una proprietà di classe.Matrice come proprietà di classe?
abstract class something {
protected $_conexion;
protected $_myArray = array();
}
Più tardi, sui metodi di questa classe, io poi usare:
$this->_myArray["action"] = "somestring";
(Dove "azione" è la chiave che deve essere inviato questa API);
Va bene? Non ho visto abbastanza OOP davanti ai miei occhi per questo lo sto chiedendo.
Come richiesto, qui è più informazioni sulle 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;
}
}
}
Grazie mille, MEM
Beh, non so esattamente perché è necessario che questa chiave di array esista, ma sicuramente, mi sembra perfettamente a posto – Hannes
Non ci sono abbastanza informazioni da dire se la proprietà '$ _myArray' è la cosa giusta da fare. I dati memorizzati in esso devono persistere attraverso chiamate di metodo 'qualcosa'? Dicci di più sull'API che stai utilizzando. – outis
@outis - Non sono sicuro della parte persistente. Quella proprietà è usata su tutti i metodi, ma i valori di quella proprietà, almeno fino ad ora, non sembrano passare (lo stesso valore) da un metodo a un altro. (era questo quello che chiedevi?) – MEM