R
R
runprogr2019-11-06 13:20:50
PHP
runprogr, 2019-11-06 13:20:50

How to properly set properties for php classes?

Suppose the class Test has properties
public $name;
public $subname;
You can directly set the values
​​$test = new Test;
$test->name = 'name';
$test->subname = 'subname';
But I noticed that most often functions are created in the class, for example
public function setName($name) {
$this->name = $name;
}
And the value is set via the
$test->setName('name');
Why is this done and how to do it right?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Romashkan, 2019-11-06
@runprogr

Correct - to set properties in the class constructor. And public fields or get/set methods are a violation of encapsulation.
An instance of a class must fully work and have all the data necessary for work immediately after its creation.
There are exceptions when we imitate data structures through DTO classes (There are no built-in types of structures / data classes in PHP), but the filling must be there through the constructor.
A lot of things are written on the Toaster, you should not believe everything, or rather, you should not believe anything, but analyze :)
In terms of some specific terms, you can often find out a lot of interesting things by googling the original source of the term / history of appearance (including the reasons).
Try to identify goals and understand how these options help you achieve those goals. And there can be no "correct" options without a given goal.
In the future, in this regard, it is worth looking at the Coupling / Cohesion concepts, and why they are important (books Clean Architecture, Clean Code, etc., the Low Coupling + High Cohesion principle is also included in the GRASP patterns ).
On topic - by setting parameters via set() methods we:
- We assume that all users of our class know what its parameters are, and moreover, what they mean. This creates a heavy load on the users of the class, complicates the client code.
- We allow in any place where the class is used to change something inside, and unexpectedly find errors in other places of the system. Also, settings can conflict with each other, and some properties can depend on others. Setting one field may require setting another, and such things should be specified at the interface level, that is, instead of the conditional setStartDate() and setEndDate(), the setDatePeriod(DatePeriod period) method should be done, even if there are two fields inside the class.
It is desirable to isolate such things, and at the level of the client code of the class, call only methods that do what we need. I think it’s not difficult to understand that the more things we need are done by a third-party module that we call, the less information we need to call the necessary methods from it, and the more control over our own validity we shift to it, the easier it is for us to use this module .
And yet - there is a difference between methods that change the state of the class, and specifically "setters". Semantics is important. The fact that something changes inside the class does not interest us. It is only important for us that what we expect from the class is fulfilled. If we want to update an article, we will call the updateArticle() method, or some changeTitle(), for example. If we want coffee, we will tell the machine makeEspresso() instead of specifying the number of beans, thus simplifying the client code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question