Answer the question
In order to leave comments, you need to log in
How to create an object with a lot of properties?
Hello.
To illustrate the issue, I will give a spherical example in a vacuum.
There is a class whose task is to write product data to a file. The write() method takes an object of the Product class as a parameter and makes sure that the object has the required properties.
Class ProductWriter
{
public function write(Product $product)
{
// write to file
}
}
Class Product
{
private $name;
private $price;
// ...
public function setName() {}
public function setPrice() {}
// ...
public function getName() {}
public function getPrice() {}
// ...
}
$product = new Product();
$product->setName('name');
$product->setPrice('0.0');
Class Product
{
function __construct($name, $price, $color, $size, ....)
{
}
}
Answer the question
In order to leave comments, you need to log in
Required parameters should be set in the class constructor - this will ensure that they are mandatory, optional using getters and setters. In addition, it is worth reconsidering whether all required fields are really required - or some are still not + if possible, parameters should be packed into value objects
Accept an associative array. Add missing properties with default values or throw an exception when a required property is missing.
See if there is a relationship between properties. Perhaps there are groups of related properties that can be separated into separate classes.
Mandatory properties - constructor parameters, the rest properties.
See also creation patterns, such as factory method and builder.
I would break the task into 2 parts: creating an object and using it.
Creating an object:
- In addition to mandatory, there can be conditionally mandatory (this is the case when setting one parameter makes a certain number of parameters mandatory) and optional.
- When all parameters are required, we write the constructor.
- It may make sense to wrap the initialization of conditionally required parameters with an auxiliary class, which can be made available only in the context of the main class, and include a check of the passed parameters in its implementation.
- The construction of classes with conditionally required and optional parameters, in addition to direct parsing of parameters, can be implemented using factories that will contain a smaller number of parameters and set the omitted ones to the correct default values.
- Instead of a factory, you can hide your class behind a facade with a simpler interface.
- Also, you can create several methods for constructing an object, and implement the constructor as a dispatcher that will call the one you need, depending on the parameters passed.
Using Objects:
- For conditionally required parameters, it is unavoidable to create methods that accept the entire tuple of related parameters. This is instead of separate setters for each property.
- In addition to set / get access methods, you can use direct access to properties, all or some, especially if there is a mutual dependence of parameters, and creating a full-fledged validator for the entire set of constructor parameters cannot be avoided.
If the answer is the most general, then I would do something like this.
$product = new Product();
$product->setData($data); // $data - массив со значениями.
// Берем ключи каждого элемента $data, приводим к camelCase, пытаемся найти такой set-метод
// в объекте product и вызвать его с относящимся к ключу значением
print_r($product->getModelErrors()); // проверяем, насколько корректно заполнен продукт
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question