Sınıf özelliği olarak dizi?

0 Cevap php

I have this API that requires me to have a specific array key to be send. Since that array needs to be used on ALL class methods, I was thinking on putting as a class property.

abstract class something {
    protected $_conexion;
    protected $_myArray = array();
}

Daha sonra, bu sınıfın yöntemleri, ben daha sonra kullanacağız:

$this->_myArray["action"] = "somestring";

(Burada "eylem" bu API için göndermek gereken anahtar);

Bu Tamam mı? Ben bu soruyorum olduğunu gözlerimin önünde yeterli OOP görmedim.

Talep olarak, burada API hakkında daha fazla bilgi olduğunu:

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;
        } 
    }
}

Thanks a lot, MEM

0 Cevap