以下「問題」PHP getter/setter到array
PHP類有很多propertys。很多Getters/Setter。
是否有任何很好的解決方案將所有屬性轉換爲數組?
protected $name;
protected $date;
public function getName();
public function getDate();
public function asArray(); // call all getters?
以下「問題」PHP getter/setter到array
PHP類有很多propertys。很多Getters/Setter。
是否有任何很好的解決方案將所有屬性轉換爲數組?
protected $name;
protected $date;
public function getName();
public function getDate();
public function asArray(); // call all getters?
是您的API已經定義了,你堅持的getX和setX的方法呢?我更喜歡屬性。減少鍵入,更好地區分屬性和方法,並且生成的代碼更像PHP,更像Java。但暴露屬性並不意味着你失去了封裝,並讓你的所有內部公開。使用__get和__set魔術方法,您可以對所呈現的內容進行非常細緻的控制。此外,這將是相當瑣碎轉儲屬性的數組:
class Foo
{
protected $properties;
public function __construct() {
$this->properties = array();
}
public function __set($prop, $value) {
$this->properties[$prop] = $value;
}
public function __get($prop) {
return $this->properties[$prop];
}
public function toArray() {
return $this->properties;
}
}
唉,如果你堅持,因爲胡思亂想老闆的制定者/吸氣或什麼OOP的一些誤解必須是,爲什麼不只是將對象轉換爲數組?
class Bar
{
public $x;
public $y;
public $z;
protected $a;
protected $b;
protected $c;
private $q;
private $r;
private $s;
public function __construct() {
}
public function setA($value) {
$this->a = $value;
}
public function getA() {
return $this->a;
}
public function setB($value) {
$this->b = $value;
}
public function getB() {
return $this->b;
}
public function setC($value) {
$this->c = $value;
}
public function getC() {
return $this->c;
}
public function toArray() {
return (array)$this;
}
}
注意如何公有,保護和私有財產投:
$bar = new Bar();
print_r($bar->toArray());
array(9) {
["x"]=>
NULL
["y"]=>
NULL
["z"]=>
NULL
[" * a"]=>
NULL
[" * b"]=>
NULL
[" * c"]=>
NULL
[" Foo q"]=>
NULL
[" Foo r"]=>
NULL
[" Foo s"]=>
NULL
}
注意,對於保護/私有數組鍵不以空格開始,這是一個空。如果你喜歡,你可以重新鍵入它們,甚至可以過濾出受保護/私有屬性:
public function toArray() {
$props = array();
foreach ((array)$this as $key => $value) {
if ($key[0] != "\0") {
$props[$key] = $value;
}
}
return $props;
}
你正在使用動態語言;利用這一點,享受它!
+1類型轉換方法。我不同意濫用__get/__設置getter/setters。 – Gordon 2010-07-10 08:55:54
您可以使用PHP的reflection功能。這裏有一個例子:
一種選擇是創建在構造函數的數組。 您將有一個getter和setter方法一個.. 當你想設置或得到的東西,這樣做:
$foo->get('UID'); //(to get user id)
or
$foo->set('UID', 5); // to set something)
嘗試尋找到get_object_vars()
,get_class_vars
和其他人在同一類別。這裏顯示的例子看起來像你所需要的。查看那裏的評論(例如http://www.php.net/manual/en/function.get-class-vars.php#87772),他們已經提供了適合您需求的方式。
+1最直接的方法imho – Gordon 2010-07-10 09:43:39
如何使用ReflectionClass和ReflectionMethod,這樣的事情:
class PropertyHolder
{
private $name;
private $date;
private $anotherProperty;
public function __construct($name, $date)
{
$this->name = $name;
$this->date = $date;
}
public function getName()
{
return $this->name;
}
public function getDate()
{
return $this->date;
}
public function asArray()
{
$result = array();
$clazz = new ReflectionClass(__CLASS__);
foreach ($clazz->getMethods() as $method) {
if (substr($method->name, 0, 3) == 'get') {
$propName = strtolower(substr($method->name, 3, 1)) . substr($method->name, 4);
$result[$propName] = $method->invoke($this);
}
}
return $result;
}
+1你擊敗了我幾乎相同的解決方案僅僅幾秒鐘:-) – Mike 2010-07-10 08:36:03
簡單(array)
投上$this
就足夠了:
(array) $this;
如果你有額外的屬性(私房例如,不得指定者()版),您可以事後取消設置這些:
public function toArray() {
$array = (array) $this;
unset($array['private'], $array['privateagain']);
return $array;
}
你問關於內置類,庫代碼還是你自己的? – Kris 2010-07-10 08:29:37