只是好奇哪种方式是正确的?
// the origional JAVA method
public void setRequestHeader(String key, String value) {
if (this.headers == null) {
this.headers = new HashMap<String, String>();
}
this.headers.put(key, value);
}
这应该在 PHP 中解释为
Class HashMap {}
/**
* @return this
*/
public function setRequestHeader($key, $value) {
if ($this->headers == NULL) {
$this->headers = new HashMap();
}
return $this->headers->$key = $value;
}
....或者....
/**
* @return array
*/
public function setRequestHeader($key, $value) {
if ($this->headers == NULL) {
$this->headers = array();
}
return $this->headers[$key] = $value;
}
如果关联数组像我相信的那样是正确的,是否需要在类的顶部声明这个变量?
// JAVA version
private HashMap<String, String> headers;
将类似于
// PHP version
private $headers = array();