PHP getter / setter for array

After the "problem"

A PHP class with many properties. Lots of getters / setter.

Is there a good solution to convert all properties to an array?

protected $name;
protected $date;

public function getName();
public function getDate();
public function asArray(); // call all getters?
+3
source share
6 answers

API , getX setX? . , PHP Java. , . __get __set magic , . , :

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

, / - - , , ?

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

; !

+9

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;
    }
+3

(array) $this:

(array) $this;

(, , Array() ed), :

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

One option is to create an array in your constructor. You will have one getter and one setter. When you want to install or get something, do something like:

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

Source: https://habr.com/ru/post/1753995/


All Articles