Php 数组作为类属性?
我有一个API,它要求我有一个要发送的特定数组密钥。 由于该数组需要在所有类方法上使用,所以我考虑将其作为类属性Php 数组作为类属性?,php,arrays,class,properties,Php,Arrays,Class,Properties,我有一个API,它要求我有一个要发送的特定数组密钥。 由于该数组需要在所有类方法上使用,所以我考虑将其作为类属性 abstract class something { protected $_conexion; protected $_myArray = array(); } 稍后,关于这个类的方法,我将使用: $this->_myArray["action"] = "somestring"; (其中“操作”是需要发送到此API的密钥) 这样行吗?我没有看到足够的OOP
abstract class something {
protected $_conexion;
protected $_myArray = array();
}
稍后,关于这个类的方法,我将使用:
$this->_myArray["action"] = "somestring";
(其中“操作”是需要发送到此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;
}
}
}
类Apiconnect{
常量URL=https://someurl.com/api.php';
const USERNAME='user';
常量密码='pass';
/**
*
*@param$postFields
*@return simplexmlement
*@desc它连接但也发送和检索以XML形式返回的信息
*/
公共函数Apiconnect($postFields)
{
$postFields[“username”]=self::username;
$postFields[“password”]=md5(self::password);
$postFields[“responsetype”]=“xml”;
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,self::URL);
卷曲设置($ch,卷曲设置桩,1);
curl_setopt($ch,CURLOPT_超时,100);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$POSTFIELDS);
$data=curl\u exec($ch);
卷曲关闭($ch);
$data=utf8\U编码($data);
$xml=新的simplexmlement($data);
如果($xml->result==“success”)
{
返回$xml;
}
其他的
{
返回$xml->message;
}
}
}
抽象类
{
受保护的$\u连接;
受保护的$_postFields=array();
/**
*@desc-Composition。
*/
公共函数构造()
{
需要一次(“apiconnect.php”);
$this->\u connection=new Apiconnect($this->\u postFields);
}
公共函数getPaymentMethods()
{
//这是需要发送的必要字段。包含API应执行的操作。
$this->_postFields[“action”]=“dosomething”;
//不确定在这里编码什么;
如果($APIREPY->result==“成功”)
{
//处理返回的XML
foreach($APIREPY->paymentmethods->paymentmethod as$method)
{
$method['module'][=$method->module;
$method['nome'][=$method->displayname;
}
返回$method;
}
}
}
非常感谢,
记忆理论
首先,一点背景知识。对象由“状态”(字段–在PHP中,这些字段通常称为“属性”,但我将以另一种方式使用该术语)和“行为”(方法)组成。状态是的一个重要部分:它允许数据在对象存在时保持不变,并允许数据在多个函数中可见。当需要数据具有这两个属性时,可以使用对象字段。这些属性是两个非常重要属性的示例:可访问性(类似于)和存储持续时间。讨论通常涉及变量(将名称与数据关联)的范围和持续时间,但这里我们将重点讨论数据
可访问性决定了代码可以在何时何地访问数据。其他类型的可访问性包括本地(其中数据只能在单个函数中访问)和全局(其中每个函数调用中的代码单元中的所有代码都可以访问数据)。与全局数据一样,状态可由多个函数访问,但与全局数据不同,在不同对象上调用时,同一方法将访问不同的数据。虚构语言中的一个例子,它多少混淆了变量和数据:
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之间的区别在于,对于状态,“between”包括没有调用任何方法的时间(即在上),而后者不包括。是否使用状态或参数取决于所需的持续时间类型。使用公共方法时,参数过多会降低可读性,并可能导致bug(如果函数为high,则更容易出现顺序错误,或者完全忘记参数)。作为次要考虑,状态可以帮助减少参数的数量
应用
从您目前所展示的情况来看,您所询问的数据不需要在方法之间访问,也不需要存在于每个方法调用之外。您询问的post字段基本上是(RPC)的参数;如果允许通过调用方法来构建这些参数,那么将数据存储为对象状态是有意义的。实际上,将post字段存储为状态是有效的,但不是最佳做法。这也不一定是最坏的做法。最好的情况是,当数据不在使用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数组设置为空数组很容易做到这一点,但此时您最好使用局部变量,而不是字段。我不知道您为什么需要此数组键存在,但请确定,我觉得很好没有足够的信息说明
$\u myArray
属性是否正确。数据是否存储