- substr: documentation ( source)
- preg_match: documentation ( source)
- ucwords: documentation ( source)
<?php
class Foo {
public static $has_many = "Post";
public function has_many($a){
return 'hola';
}
function get_relationship($model) {
$class = new ReflectionClass($this);
$staticProperties = $class->getStaticProperties();
foreach($staticProperties as $propertyName => $value) {
if(preg_match('/^(has_many|age)$/', $propertyName) && $value == $model) {
return $this->{$propertyName}($model);
}
}
}
function __get($name) {
echo $name;
if(substr($name, -strlen('_set')) === '_set') {
echo $model = ucwords(substr($name, 4));
return $this->get_relationship($model);
}
}
}
echo substr('post_set', -strlen('_set'));
$foo = new Foo;
echo $foo->post_set;
?>