Created
February 26, 2019 09:42
-
-
Save kobus1998/cc718327baacc4eb605b8cb5a7498f85 to your computer and use it in GitHub Desktop.
Get and set anything
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 | |
trait GetAndSet | |
{ | |
/** | |
* Magic method __call | |
* | |
* @param string $method | |
* @param array $params | |
* @return mixed | |
*/ | |
public function __call(string $method, array $params) | |
{ | |
if (substr($method, 0, 3) == 'get') { | |
// get property | |
$property = lcfirst(substr($method, 3)); | |
return $this->{$property}; | |
} elseif (substr($method, 0, 3) == 'set') { | |
// set property | |
$property = lcfirst(substr($method, 3)); | |
$this->{$property} = $params[0]; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment