MEM*_*MEM 9 php arrays properties class
我有这个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;
        } 
    }
}
非常感谢,MEM
首先,有点背景.对象由"状态"组成(字段 - 在PHP中,这些通常称为"属性",但我将以另一种方式使用该术语)和"行为"(方法).状态是封装的一个重要部分:它允许数据在对象存在时持久存在,并允许数据在多个函数中可见.当您需要具有这两个属性的数据时,可以使用对象字段.这些属性是两个非常重要的属性的示例:可访问性(类似于变量范围)和存储持续时间.讨论通常涵盖变量的范围和持续时间(将名称与数据相关联),但在这里我们将重点关注数据.
可访问性决定了数据可以通过代码访问的时间和位置.其他类型的可访问性包括本地(其中数据只能在单个函数中访问)和全局(其中数据可在每个函数调用中的代码单元中的所有代码中访问).与全局数据一样,state可以被多个函数访问,但与全局数据不同,当在不同对象上调用时,相同的方法将访问不同的数据.组合语言中的一个例子有点混淆变量和数据:
i=0
inc() {...}
dec() {...}
class C {
  i=0
  inc() {...}
  dec() {...}
}
a = C()
b = C()
inc() // the global i is visible in both these calls, 
dec() // which access the same data
a.inc() // C::i is visible in both C::inc and C::dec,
b.dec() // but these calls access different data
i    // The global i is accessible here
// C::i not accessible
存储持续时间决定了数据存在的时间(创建和销毁数据的时间).持续时间的类型包括自动(数据存在,直到创建它的函数退出),静态(数据存在于进程的生命周期)和动态(数据被显式创建,并且当没有时,由垃圾收集器显式销毁或销毁更长的访问).状态与对象共享持续时间:如果对象是自动的,则状态是自动的; 如果动态,国家是动态的.
状态不是在方法调用之间访问数据的唯一方法.您还可以将数据作为参数传递给方法,在这种情况下,数据具有本地持续时间.to之间的区别在于,对于state,"between"包括没有调用方法的时间(即在调用堆栈上),而后者则没有.是否使用状态或参数取决于所需的持续时间类型.随着公共方法,有太多的争论减少了可读性,并可能导致错误(具有高的函数元数,它更容易获得订单错了,或者完全忘记了一个参数).作为次要考虑因素,州可以帮助减少参数的数量.
根据您到目前为止所显示的内容,您询问的数据不需要在方法之间访问,也不需要在每个方法调用之外存在.您询问的帖子字段基本上是远程过程调用(RPC)的参数; 如果您允许通过调用方法来构建这些参数,那么将数据存储为对象状态是有意义的.实际上,将帖子字段存储为状态是有效的,但不是最佳实践.这也不一定是最糟糕的做法.最好的情况是,当不在使用API的方法中时,通过保持数据存在,使对象混乱并浪费内存.最糟糕的情况是,在一个方法中设置参数,然后在调用另一个方法时在RPC中传递.
abstract class ApiSomething {
    public function eatSaltyPork() {
        $this->_postFields["action"] = __FUNCTION__;
        $this->_postFields['spices[]'] = 'salt';
        $result = $this->_connection->Apiconnect($this->_postFields);
        ...
    }
    public function eachCheese() {
        $this->_postFields["action"] = __FUNCTION__;
        $result = $this->_connection->Apiconnect($this->_postFields);
        ...
    }
}
$thing = new ApiSomething();
$thing->eatSaltyPork();
$thing->eatCheese(); // ends up eating salty cheese
这是你想要避免的事情.可以通过将post fields数组设置为空数组来轻松完成,但此时您也可以使用局部变量而不是字段.