Skip to content

Instantly share code, notes, and snippets.

@kobus1998
Created February 26, 2019 09:42
Show Gist options
  • Save kobus1998/cc718327baacc4eb605b8cb5a7498f85 to your computer and use it in GitHub Desktop.
Save kobus1998/cc718327baacc4eb605b8cb5a7498f85 to your computer and use it in GitHub Desktop.
Get and set anything
<?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