function FetchModeTrait::assocToClass

Same name and namespace in other branches
  1. 10 core/lib/Drupal/Core/Database/FetchModeTrait.php \Drupal\Core\Database\FetchModeTrait::assocToClass()

Converts a row of data in FETCH_ASSOC format to FETCH_CLASS.

Parameters

array $rowAssoc: A row of data in FETCH_ASSOC format.

string $className: Name of the created class.

array $constructorArguments: Elements of this array are passed to the constructor.

Return value

object The row in FETCH_CLASS format.

File

core/lib/Drupal/Core/Database/FetchModeTrait.php, line 84

Class

FetchModeTrait
Provide helper methods for statement fetching.

Namespace

Drupal\Core\Database

Code

protected function assocToClass(array $rowAssoc, string $className, array $constructorArguments) : object {
    $classObj = new $className(...$constructorArguments);
    foreach ($rowAssoc as $column => $value) {
        $classObj->{$column} = $value;
    }
    return $classObj;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.