Skip to content

Instantly share code, notes, and snippets.

@gong023
Created October 8, 2015 09:11
Show Gist options
  • Save gong023/9d25d3bddc3c180e40b2 to your computer and use it in GitHub Desktop.
Save gong023/9d25d3bddc3c180e40b2 to your computer and use it in GitHub Desktop.
magicSpirce fake
<?php
trait EntityTrait
{
public function __construct(array $properties = [])
{
foreach ($properties as $k => $v) {
$this->{$k} = $v;
}
}
/**
* @return int|null
*/
private function getInt()
{
static $property_name;
if ($property_name === null) {
$property_name = $this->getCalledPropertyName();
}
return isset($this->{$property_name}) ? (int)$this->{$property_name} : null;
}
/**
* @return float|null
*/
private function getFloat()
{
static $property_name;
if ($property_name === null) {
$property_name = $this->getCalledPropertyName();
}
return isset($this->{$property_name}) ? (float)$this->{$property_name} : null;
}
/**
* @return string|null
*/
private function getString()
{
static $property_name;
if ($property_name === null) {
$property_name = $this->getCalledPropertyName();
}
return isset($this->{$property_name}) ? (string)$this->{$property_name} : null;
}
/*
* @return string
*/
protected function getCalledPropertyName()
{
return strtolower(preg_replace(
'/[A-Z]/',
'_$0',
lcfirst(preg_replace('/^(get|set)/', '', debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['function']))
));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment