function FetchModeTrait::assocToClass

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

Converts a row of data in associative format to classed object.

Parameters

array $rowAssoc: A row of data in associative 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 classed object format.

File

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

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.