# 构建对象到数组到转化器

这个案例与前一个刚好相反。在这种情况下，我们需要从对象属性中提取值，并返回一个关联数组，其中键是列名。

## 如何做...

1.在这个例子中，我们将建立在前面的案例中定义的`Application\Generic\Hydrator\GetSet`类的基础上。

```php
namespace Application\Generic\Hydrator;
class GetSet
{
  // code
}
```

2.在前面的配方中定义了`hydrate()`方法之后，我们定义了一个`extract()`方法，它以一个对象作为参数。其逻辑与`hydrate()`方法类似，只是这次我们搜索的是`getXXX()`方法。再次使用 `preg_match()` 来匹配方法的前缀和后缀，后缀被认为是数组键。

```php
public static function extract($object)
{
  $array = array();
  $class = get_class($object);
  $methodList = get_class_methods($class);
  foreach ($methodList as $method) {
    preg_match('/^(get)(.*?)$/i', $method, $matches);
    $prefix = $matches[1] ?? '';
    $key    = $matches[2] ?? '';
    $key    = strtolower(substr($key, 0, 1)) . substr($key, 1);
    if ($prefix == 'get') {
      $array[$key] = $object->$method();
    }
  }
  return $array;
}
}
```

{% hint style="info" %}
请注意，为了方便起见，我们将 `hydrate()` 和 `extract()` 定义为静态方法。
{% endhint %}

## 如何运行...

定义一个名为`chap_11_object_to_array.php`的调用程序，设置自动加载，并使用相应的类。

```php
<?php
require __DIR__ . '/../Application/Autoload/Loader.php';
Application\Autoload\Loader::init(__DIR__ . '/..');
use Application\Entity\Person;
use Application\Generic\Hydrator\GetSet;
```

接下来，定义一个 `Person` 的实例，为其属性设置值。

```php
$obj = new Person();
$obj->setFirstName('Li\'lAbner');
$obj->setLastName('Yokum');
$obj->setAddress('1DirtStreet');
$obj->setCity('Dogpatch');
$obj->setStateProv('Kentucky');
$obj->setPostalCode('12345');
$obj->setCountry('USA');
```

最后，以静态方式调用 new `extract()`方法。

```php
$a = GetSet::extract($obj);
var_dump($a);
```

输出结果如下截图所示。

![](/files/-MOQNShEXGsu95iQIV7S)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ganymedenil.gitbook.io/php-7/mo-kuai-yi/di-shi-yi-zhang-ruan-jian-she-ji-mo-shi-de-shi-xian/gou-jian-dui-xiang-dao-shu-zu-dao-zhuan-hua-qi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
