It is not a good idea to use magic methods as __get() and __set(). The first reason being that the code doesn’t communicate its intend clearly. Secondly, a client will have to know about which fields are available to be able to use the magic methods. This violates the Law of Demeter that says a module shouldn’t know about the innards of the objects it manipulates.
Accessors, mutators and predicates should be named for their value and prefixed with get, set, and is.
For example:
MyClass
{
/** @var int */
private $myField;
/** @return int */
protected function getMyField()
{
$this->myField;
}
/** @param int $myField */
protected function setMyField($myField)
{
$this->myField = $myField;
}
}
You should by all means try to avoid creating public mutators, as it is tempting for external functions to use them the way a procedural program would use a data structure and thereby manipulate the internal state of the object, known as Feature Envy.
The methods of a class should only be interested in the fields and methods of the class they belong to, and not the fields and methods of other classes. When a method uses accessors and mutators of another object to manipulate the data within that object, then it envies the scope of that object. So we want to eliminate Feature Envy because it exposes the internals of one class to another. Sometimes, however, Feature Envy is a necessary evil, e.g., in the case where one object’s method requires the another object’s data to act upon, and moving that object’s method into the object holding the data would violate principles of object oriented design.
Why then should we have accessors and mutators if other classes shouldn’t use them? Well, making accessors and mutators protected allows for derived classes to access the parent’s private fields, yet still shielding the objects internal state from external peeking and poking.
But couldn’t we then just set the field to protected and thereby eliminating excess methods of the class? We surely could, but then you don’t have the possibility to shield the field from being set by a subclass.
In PHP, we could even argue that it makes sense for a class to use private mutators, so we have the opportunity to type check the value before it is set on the field.
For example:
MyClass
{
/** @var int */
private $myField;
/** @return int */
protected function getMyField()
{
$this->myField;
}
/** @param int $myField */
private function setMyField($myField)
{
if (!(is_int($myField) || null === $myField)) {
throw new \InvalidArgumentException(sprintf(
'%s expects $myField to be integer; received %s.'
__METHOD__,
gettype($myField)
));
}
$this->myField = $myField;
}
}
Here an exception is thrown if $myField is not an integer or null. However, it could also be argued that such code is a sign of mistrust to own coding. Regardless, having a private mutator can easily be changed to protected to allow for subclass manipulation of the field, and the accessor method could be set to private or removed to prevent subclasses reading the field.
$foo(members should all be private, right?) and you wanted an accessor to the member$foo, but only want that class to be able to access it. So you would need aprivate function get_foo(), which is a private property.$this->property. Getters and setters are meant to be used when value of a variable is set from out side the class to get its value. If you really need a private function, why use magic function? . you can name it something else.