Here is a clean version of what you wanted:
class ClassName {
public static function init(){
return (new ReflectionClass(get_called_class()))->newInstanceArgs(func_get_args());
}
public static function initArray($array=[]){
return (new ReflectionClass(get_called_class()))->newInstanceArgs($array);
}
public function __construct($arg1, $arg2, $arg3){
///construction code
}
}
Normal ugly method of creating a new object instance using new
$obj = new ClassName('arg1', 'arg2', 'arg3');
echo $obj->method1()->method2();
Static call using init instead of new
echo ClassName::init('arg1', 'arg2', 'arg3')->method1()->method2();
Static call using initArray instead of new
echo ClassName::initArray(['arg1', 'arg2', 'arg3'])->method1()->method2();
$paramto a constructor for a class that doesn't need it. If, on the other hand, you are saying that the type of object that gets created is actually dependant on$param, then you'll want to look into something like the Factory Pattern: en.wikipedia.org/wiki/Factory_method_pattern. PHP can't handle your construction decisions for you in that case... you'll need to write logic to decide what gets built.