📜 ⬆️ ⬇️

Public properties, getters and setters or magic methods?

As a rule, opinions differ on whether it is good practice to use public properties in PHP classes, or should you use getters and setters (and keep properties private or protected). Another compromise is to use the __get() and __set() magic methods.
Each approach has its advantages and disadvantages, let's take a look at them ...


An example of using public properties:
 class Foo { public $bar; } $foo = new Foo(); $foo->bar = 1; $foo->bar++; 

In this example, bar is a public property of the class Foo . With this approach, we can manipulate this property as you please and store any data in it.

Advantages of public properties

The disadvantages of public properties

')
An example of using getters and setters:
 class Foo { private $bar; public function getBar() { return $this->bar; } public function setBar($bar) { $this->bar = $bar; } } $foo = new Foo(); $foo->setBar(1); $foo->setBar($foo->getBar() + 1); 

The bar property here is private; therefore, it cannot be accessed directly. In order to get the value of a property, you have to use the getBar method, or setBar to assign a value to a property. So that you can be sure that the input data is completely correct, these methods can include the appropriate functionality for their validation.

Advantages of getters and setters

Disadvantages getters and setters


An example of the use of magic getters and setters:
 class Foo { protected $bar; public function __get($property) { switch ($property) { case 'bar': return $this->bar; //etc. } } public function __set($property, $value) { switch ($property) { case 'bar': $this->bar = $value; break; //etc. } } } $foo = new Foo(); $foo->bar = 1; $foo->bar++; 

In this case, the bar property is not public, but in the code it is used as if it were public. When PHP cannot find the corresponding public property, it calls the appropriate magic method ( __get() to get the value, __set() to set the value). This approach may seem like the golden mean, but it has a significant drawback (see shortcomings below!). It should also be noted that __get() and __set() methods are NOT called for public properties, and are called if the property is marked as protected or private and is outside the scope, or if the property is not defined.

Advantages of magical getters and setters

Disadvantages of magic getters and setters


Which approach to use
Obviously, getters and setters have a number of significant advantages, and some people think that they should be used all the time (especially those with a past Java connection!). But in my opinion, by doing so they disrupt the natural development of the language, and their excessive complexity and detail force me to work with it, even if there is no need for it (it annoys me when ordinary getters and setters DO NOT do anything except get and set the property). In such cases, I try to use public properties, and I use getters and setters for critical properties that need to be more strictly controlled, or if they need to use deferred data loading.

Other alternatives?
Before learning PHP, I used C #. In C #, all properties have access methods, but you do not need to call them as methods, you can manipulate your properties directly and the corresponding methods will be called magically. This is in some ways similar to the magic __get() and __set() methods in PHP, but the properties remain defined and accessible. This is the golden mean and it would be very nice to see a similar opportunity in PHP.

Sadly, however, the RFC necessary for implementing C # of a similar syntax for defining methods for accessing properties did not receive the required two-thirds of the vote: wiki.php.net/rfc/propertygetsetsyntax-v1.2 That's it!

Source: https://habr.com/ru/post/197332/


All Articles