@ 2014-10-08T05:53:51Z <?php
trait LazyGetSet
{
private $useFluentSetter = true;
/**
* Change fluent setter.
*/
public function setFluentSetter($flag)
{
$this->useFluentSetter = $flag;
}
public function __call($method, $args)
{
if ((0 === strpos($method, 'get')) && ($ptyName = $this->getLazyPropertyName($method))) {
return $this->lazyGet($ptyName, $args);
}
if ((0 === strpos($method, 'set')) && ($ptyName = $this->getLazyPropertyName($method))) {
return $this->lazySet($ptyName, $args);
}
}
/**
* Detect property name from setter/getter methods.
*/
private function getLazyPropertyName($method)
{
$ptyName = lcfirst(substr($method, 3));
if (property_exists($this, $ptyName)) {
return $ptyName;
}
}
/**
* Get property value
*/
private function lazyGet($ptyName)
{
return $this->{$ptyName};
}
/**
* Inject property value.
*
* @todo Validate data type
*/
private function lazySet($ptyName, $args)
{
if($this->validate($ptyName, $args[0])) {
$this->{$ptyName} = $args[0];
}
if ($this->useFluentSetter) {
return $this;
}
}
private function validate($variable_name, $variable_value) {
$hints = $this->getPropertyHintType(__CLASS__, $variable_name);
if(isset($hints['@var'])) {
$validateCallback = array(
'string' => 'is_string',
'integer' => 'is_integer',
'array' => 'is_array',
'float' => 'is_float',
'double' => 'is_double',
'boolean' => 'is_bool',
'object' => 'is_object',
'resource' => 'is_resource',
);
if(isset($validateCallback[$hints['@var']])) {
return call_user_func($validateCallback[$hints['@var']], $variable_value);
} else {
return $variable_value instanceof $hints['@var'];
}
}
}
public function getPropertyHintType($className, $propertyName) {
$class = new ReflectionClass($className);
$match = array();
$docs = array();
foreach(explode("\n",(str_replace(array('*', '/'),'', $class->getProperty($propertyName)->getDocComment()))) as $doc) {
$docs[] = trim($doc);
}
$return = array();
foreach(array_filter($docs) as $doc) {
$match = array();
preg_match('/^([@].*)\ (.*)$/', $doc, $match);
if($match){
$return[$match[1]] = trim($match[2]);
}
}
return $return;
}
}
namespace quyentest;
class MyClass
{
use \LazyGetSet;
/**
* @var integer
*/
private $name;
/**
* @var MyClass
*/
private $instant;
}
//echo (new MyClass())->setName(123)->getName();
$MyClass = new MyClass();
echo $MyClass->setInstant('a')->getInstant();
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 7.2.29 - 7.2.33 , 7.3.16 - 7.3.33 , 8.0.0 - 8.0.30 , 8.1.0 - 8.1.27 , 8.2.0 - 8.2.17 , 8.3.0 - 8.3.4 Fatal error: Namespace declaration statement has to be the very first statement or after any declare call in the script in /in/8i52E on line 103
Process exited with code 255 . Output for 7.4.3 - 7.4.33 Output for 5.4.0 - 5.4.32 Fatal error: Namespace declaration statement has to be the very first statement in the script in /in/8i52E on line 103
Process exited with code 255 . Output for 4.4.2 - 4.4.9 , 5.1.0 - 5.1.6 , 5.2.0 - 5.2.17 , 5.3.0 - 5.3.29 Parse error: syntax error, unexpected T_STRING in /in/8i52E on line 3
Process exited with code 255 . Output for 4.3.0 - 4.3.1 , 4.3.5 - 4.3.11 , 4.4.0 - 4.4.1 , 5.0.0 - 5.0.5 Parse error: parse error, unexpected T_STRING in /in/8i52E on line 3
Process exited with code 255 . Output for 4.3.2 - 4.3.4 Parse error: parse error in /in/8i52E on line 3
Process exited with code 255 . preferences:dark mode live preview
245.92 ms | 401 KiB | 251 Q