Last active
December 21, 2015 03:09
-
-
Save ChrisBuchholz/6240407 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class Synthesization { | |
| private function signatureIsGetter($signature) { | |
| if (preg_match('/^get/', $signature)) | |
| return true; | |
| return false; | |
| } | |
| private function signatureIsSetter($signature) { | |
| if (preg_match('/^set/', $signature)) | |
| return true; | |
| return false; | |
| } | |
| private function getPropertyFromMethodSignature($signature) { | |
| return lcfirst(preg_replace('/^(g|s)et/', '', $signature)); | |
| } | |
| public function __call($method, $params) { | |
| $property = $this->getPropertyFromMethodSignature($method); | |
| if (property_exists($this, $property)) { | |
| if ($this->signatureIsGetter($method)) | |
| return $this->{$property}; | |
| else if ($this->signatureIsSetter($method)) | |
| return $this->{$property} = $params[0]; | |
| } | |
| return false; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class Synthesization { | |
| private function signatureIsGetter($signature) { | |
| return preg_match('/^get/', $signature) ? true : false; | |
| } | |
| private function signatureIsSetter($signature) { | |
| return preg_match('/^set/', $signature) ? true : false; | |
| } | |
| private function getPropertyFromMethodSignature($signature) { | |
| return lcfirst(preg_replace('/^(g|s)et/', '', $signature)); | |
| } | |
| public function __call($method, $params) { | |
| $property = $this->getPropertyFromMethodSignature($method); | |
| $reflector = new ReflectionClass(get_class($this)); | |
| if ($reflector->hasProperty($property)) { | |
| $prop = $reflector->getProperty($property); | |
| $prop->setAccessible(true); | |
| if ($this->signatureIsGetter($method)) | |
| return $prop->getValue($this); | |
| else if ($this->signatureIsSetter($method)) | |
| return $prop->setValue($this, $params[0]); | |
| $prop->setAccessible(false); | |
| } | |
| return false; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example use case: