- class_exists: documentation ( source)
- array_shift: documentation ( source)
<?php
class A {
private $foo;
public function __construct($foo) {
$this->foo = $foo;
}
public function foo() {
return $this->foo;
}
public function Hello() {
return 'Hello World!';
}
}
function PHP(...$args) {
if(isset($args[0]) && !empty($args[0])) {
$class = $args[0];
if(class_exists($class)) {
$string = '$obj = new '.$class;
if(isset($args[1])) {
$arguments = $args;
$string .= '('.$args[1];
array_shift($arguments);
array_shift($arguments);
foreach($arguments as $arg) {
if(is_string($arg)) {
$string .= ', \''.$arg.'\'';
}
else {
$string .= ', '.$arg;
}
}
$string .= ')';
}
$string .= ';';
eval($string);
return $obj;
}
else {
print('This class do not exists.');
}
}
else {
print('You must a define a class to set as object.');
}
}
$obj = PHP('A', 'foo');
echo($obj->foo());