How I redefined my Getters & Setters Methods with Enums
PHP Enums came in with 8.1 and it has been a very groundbreaking feature introduced into PHP. For me, the getters and setters approach has been modified. Some people agree that the Getters and Setters system is outdated since the defined data type for objects came out, but I still find myself trying to run some line of instructions before assigning data to an object, I still use the setters. But instead of having setPhone() and getPhone() methods in the code, I prefer just phone()
acting as both getter and setter. How do I achieve this? Enums!
Currently, this is what the getter and setter code looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
<?php class SampleEntity { private string $name; private int $age; /** * @return string */ public function getName(): string { return $this->name; } /** * @param string $name * @return SampleEntity */ public function setName(string $name): SampleEntity { $this->name = $name; return $this; } /** * @return int */ public function getAge(): int { return $this->age; } /** * @param int $age * @return SampleEntity */ public function setAge(int $age): SampleEntity { $this->age = $age; return $this; } } |
The Close Call
I could try to use the new idea to merge both the getter and setter into one method without using Enum:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
/** * @param string|null $name * @return SampleEntity|string */ public function name(?string $name = null): SampleEntity|string { if ($name === null) { return $this->name; } $this->name = $name; return $this; } /** * @param int|null $age * @return SampleEntity|int */ public function age(?int $age = null): SampleEntity|int { if ($age === null) { return $this->age; } $this->age = $age; return $this; } |
This is really close to the idea. But the flaw here is that; what happens when you have a nullable property? The setter will always run as a getter because it is passing its default value. This is where Enums come in, the unique data type!
The Call
To explain this, we depend on the uniqueness of the Enumerations data type, unlike null or any other data type you might choose, you might just run into a conflict in the future when a new property accepts that data type. So, passing a default value of an enum with its uniqueness can be used to control the behavior of a method, based on a condition.
To put this in action, we have to create the enum.
1 2 3 4 5 |
<?php enum MethodAction { case GET; } |
Then the modified methods:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
<?php class SampleEntity { private string $name; private int $age; /** * @param string|MethodAction $name * @return SampleEntity|string */ public function name(string|MethodAction $name = MethodAction::GET): SampleEntity|string { if ($name === MethodAction::GET) { return $this->name; } $this->name = $name; return $this; } /** * @param int|MethodAction $age * @return SampleEntity|int */ public function age(int|MethodAction $age = MethodAction::GET): SampleEntity|int { if ($age === MethodAction::GET) { return $this->age; } $this->age = $age; return $this; } } |
The Result
Now we have one method for both getting and setting, which can be expressed like this:
1 2 3 4 5 6 |
<?php $entity = new SampleEntity(); $entity->age(20); $entity->name('James'); printf('%s is %d years old', $entity->name(), $entity->age()); |
Let me know your thoughts!