Improve modification constructor to work with values with non-default values as well
Currently, table->new()
method takes all required values (which means those which have no default value). This leads to a problem that you can have e.g. five values related to user's name (2x degree, first name, middle name, last name; only first and last name are obligatory), but you have to split the whole group to two parts – obligatory and optional – which is not convenient:
$user = $this->usersTable->new($firstName, $lastName);
// ...
$user->modifyDegreeBefore($degreeBefore);
$user->modifyMiddleName($middleName);
$user->modifyDegreeAfter($degreeAfter);
We can make several things about it:
- let modification class, which is called in table's
new()
method, to have constructor with parameters as well –from()
- create two types of constructor – one for all fields and one for required only fields –
from()
&fromMandatory()
; in the "all fields" constructor we can take advantage of PHP8's named arguments to pass optional fields if not needed (but we have to deal with default value detection first) - use modifications directly and deprecate table's API (maybe not good idea?)
$user = UserModification::from(firstName: $firstName, middleName: $middleName, lastName: $lastName);
Edited by Daniel Kurowski