2017 © Pedro Peláez
 

library construct-named-parameters

Helper functions to create objects using an array as named parameters

image

eclipxe/construct-named-parameters

Helper functions to create objects using an array as named parameters

  • Wednesday, June 21, 2017
  • by eclipxe
  • Repository
  • 1 Watchers
  • 0 Stars
  • 19 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 36 % Grown

The README.md

eclipxe/construct-named-parameters - Helper functions to create objects using an array as named parameters

![Source Code][badge-source] Latest Version ![Software License][badge-license] Build Status [![Scrutinizer][badge-quality]][quality] ![Coverage Status][badge-coverage] Total Downloads SensioLabsInsight, (*1)

PHP does not have the option to call a method or instance an object using named parameters. This is a library I use in other projects to simplify the task of create objects based on named parameters., (*2)

See PHP RFC: Named Parameters, (*3)

Instalation

Use composer to install this library composer require eclipxe/construct-named-parameters, (*4)

Basic use

<?php

class Foo
{
    public function __construct($a, $b, $c, $d) {}
}

$foo = construct_named_parameters(Foo::class, [
    'd' => 123,
    'b' => 'second',
    'a' => 'a argument',
    'c' => false,
    'xtra' => 'baz',
]);

// this will return the result of:
new Foo('a argument', 'second', false, 123);

Why this library exists?

My motivation start in a project that contains a lot of domain objects that need to be instantiated and commonly I had all the information in one single array. This library result very useful in those cases., (*5)

It even contains a method that put all parameters and values as lowercase to easily match the constructor requirement. Be aware that in some weird cases a constructor can contain two parameters with the same name but in different case., (*6)

How it knows the constructor arguments?

The only way I found is using \Reflection., (*7)

The bad is that reflection can be expensive., (*8)

The good part is that the function \ConstructNamedParameters\Builder::retrieveArguments has a cache of the name of the class, so, it will only investigate the constructor the first time and the second will use the cached information., (*9)

The cache can not be cleaned or forced to reload, and there is no need to do it. A constructor of a class cannot change once it exists, right?, (*10)

About the public functions:

Public function Actual calls
construct_named_parameters \ConstructNamedParameters\Builder::create
construct_named_parameters_uncase \ConstructNamedParameters\Builder::createIgnoreCase
constructor_arguments \ConstructNamedParameters\Builder::retrieveArguments

Contributing

Contributions are welcome! Please read CONTRIBUTING for details and don't forget to take a look in the TODO and CHANGELOG files., (*11)

The construct-named-parameters library is copyright © Carlos C Soto and licensed for use under the MIT License (MIT). Please see LICENSE for more information., (*12)

The Versions

21/06 2017

dev-master

9999999-dev https://github.com/eclipxe13/construct-named-parameters

Helper functions to create objects using an array as named parameters

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

named parameters

21/06 2017

v1.1.0

1.1.0.0 https://github.com/eclipxe13/construct-named-parameters

Helper functions to create objects using an array as named parameters

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

named parameters

16/06 2017

dev-development

dev-development https://github.com/eclipxe13/construct-named-parameters

Helper functions to create objects using an array as named parameters

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

named parameters

15/06 2017

v1.0.0

1.0.0.0 https://github.com/eclipxe13/construct-named-parameters

Helper functions to create objects using an array as named parameters

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

named parameters