Calculates the default value based on variable type, including fully-qualified classnames.

0.0.3 2021-03-31 21:44 UTC

This package is auto-updated.

Last update: 2021-08-31 00:41:21 UTC


A small utility to return a default value or instance based on a variable type or classname.

Vanilla PHP


$variable_types = [

  // In addition you can pass fully-qualified class names, so long as their
  // constructors do not REQUIRE any parameters.

foreach ($variable_types as $variable_type) {
  $default_value = \AKlump\DefaultValue\DefaultValue::get($variable_type);

Drupal 8+ Integration

When using within a Drupal installation use the class \Drupal\Component\Utility\DefaultValue and you'll get special Drupal support, in addition to the vanilla PHP explained above.


$special_drupal_variable_types = [

  // This is a service ID.
  // This class has a ::create method with no required arguments.

  // This class implements ContainerInjectionInterface.

foreach ($special_drupal_variable_types as $variable_type) {
  $default_value = \Drupal\Component\Utility\DefaultValue::get($variable_type);