Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP getter/setter到数组_Php_Oop - Fatal编程技术网

PHP getter/setter到数组

PHP getter/setter到数组,php,oop,Php,Oop,“问题”之后 具有许多属性的PHP类。很多能手/二传手 有没有什么好的解决方案可以将所有属性转换为一个数组 protected $name; protected $date; public function getName(); public function getDate(); public function asArray(); // call all getters? class Bar { public $x; public $y; public $z;

“问题”之后

具有许多属性的PHP类。很多能手/二传手

有没有什么好的解决方案可以将所有属性转换为一个数组

protected $name;
protected $date;

public function getName();
public function getDate();
public function asArray(); // call all getters?
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;
    }
}
您可以使用PHP的功能。下面是一个例子:


一个选项是在构造函数中创建一个数组。 你将有一个能手和一个二传手。。 当您想要设置或获取某些内容时,请执行以下操作:

$foo->get( 'UID' ); //(to get user id)
or
$foo->set( 'UID', 5 ); // to set something)

试着调查,和其他同类的人。这里显示的示例与您需要的非常相似。检查那里的注释(例如),它们已经提供了适合您需要的方法。

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

您的API已经定义了吗?您是否坚持使用getX和setX方法?我更喜欢房地产。类型更少,属性和方法之间的区别更好,生成的代码看起来更像PHP,而不像Java。但是,公开属性并不意味着失去封装并公开所有内部内容。使用uuu get和uuu 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必须是什么的一些误解而被setter/getter困住了,为什么不直接将对象转换成数组呢

protected $name;
protected $date;

public function getName();
public function getDate();
public function asArray(); // call all getters?
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
}
请注意,protected/private的数组键不是以空格开头的,而是null。如果愿意,您可以为它们重新设置密钥,甚至过滤掉受保护/私有属性:

public function toArray() {
    $props = array();
    foreach ((array)$this as $key => $value) {
        if ($key[0] != "\0") {
            $props[$key] = $value;
        }
    }
    return $props;
}
您使用的是动态语言;好好利用它,享受它

一个简单的
(数组)
$this
上转换就足够了:

(array) $this;
如果您有其他属性(例如,私人属性,不应设置为数组()),您可以随后取消设置这些属性:

public function toArray() {
    $array = (array) $this;
    unset($array['private'], $array['privateagain']);
    return $array;
}

你是在问一个内置类、库代码还是你自己的?+1你在一个几乎相同的解决方案上只比我领先了几秒钟:-)对于类型转换方法+1。我不同意错误地使用getter/setter的uuu get/uu set。我并不真的不同意你的观点,但是……使用这种代码编写,你会失去一些自动完成帮助。当您使用IDE时,它非常有用…