Skip to main content

Introducing PHP Model Builders

· 3 min read

We have introduced builder pattern in PHP SDKs that can be used to set all model properties at once and then build the object at the end.

Details

We have changed how any custom type is created for the better in PHP SDKs.

Previously, Custom type instantiation was confusing. Let's take a look at an example to understand better:

$requiredField = 'not nullable and required';
$requiredFieldSecond = 'not nullable and required second';
$model = new Model(
$requiredField,
$requiredFieldSecond
);
$model->setRequiredNullable('Required_Nullable_Field_Value');

Let's say we have a Model, as shown above with 1 required non-nullable properties, and the other property is required but nullable. In this case, what we had to do was:

  1. Create the custom type with all the required properties set through the constructor.
  2. Call the setter for the nullable required property separately after the creation of the main object.

It took quite a few number of lines to create an object like this. Plus, if the object hierarchy becomes complex, it made the code hard to understand for the developer, i.e. the case of object within an object and so on. In such a case, what we had to do before was to create the child object first before creating the parent object. And if that child in turn has another object within it, then good luck understanding the long piece of code.

What has changed

Now, with the introduction of the builder pattern you can set all the properties at once and then build the object at the end. Let's see it in action with an example, by changing the code above to utilize builder methods:

$model = ModelBuilder::init(
'not nullable and required',
'not nullable and required second'
)
->requiredNullable('Required_Nullable_Field_Value')
->build();

The required properties are being set more or less the same way, other than the use of init method. The most prominent change is the inline initialization of optional or nullable properties, and this is what allows us to initialize a custom type, along with its nested types in one go without the need of any prior creation of variables.

So let's say if our class Model had another property named innerModel which was also a custom type, how would it have looked in the old and the new code samples? Let's check it out:

Without Model Builders

$requiredField = 'not nullable and required';
$requiredFieldSecond = 'not nullable and required second';
$innerModel = new Models\InnerModel('modelName');
$innerModel->setIntProperty(12);
$model = new Models\Model(
$requiredField,
$requiredFieldSecond
);
$model->setRequiredNullable('Required_Nullable_Field_Value');
$model->setInnerModel($innerModel);

With Model Builders

$model = ModelBuilder::init(
'not nullable and required',
'not nullable and required second'
)
->requiredNullable('Required_Nullable_Field_Value')
->innerModel(
InnerModelBuilder::init('modelName')
->intProperty(12)
->build()
)
->build();

Notice how multiple lines of code along with the use of a separate variable was replaced with an inline more readable, idiomatic code. That is what the model builders provide us with.

In short, the change involves implementing the builder pattern to create all custom type objects. The new builder class provides methods to initialize and set the nullable properties of the object, resulting in more concise, readable and idiomatic code.