Skip to content

Instantly share code, notes, and snippets.

@antonioribeiro
Created January 19, 2017 20:19
Show Gist options
  • Save antonioribeiro/69759aec979d27d49beec21acbbdee86 to your computer and use it in GitHub Desktop.
Save antonioribeiro/69759aec979d27d49beec21acbbdee86 to your computer and use it in GitHub Desktop.
// Instead of
$instance1 = app($class, $parameters);
// or
$instance2 = app()->make($class, $parameters);
// or
$instance3 = Container::getInstance()->make($class, $parameters);
// You may have to do something like
$instance4 = new $class($parameters[0]);
// but..., if you have more than one constructor argument, you may
$reflection = new ReflectionClass($class);
$instance5 = $reflection->newInstanceArgs($parameters);
// or you can just go Vanilla PHP and do
$instance6 = new YourClass($whatever, $parameter);
@AdenFraser
Copy link

You can also use the splat operator, which some might find more elegant than listing out multiple arguments without using the overhead of reflection.

$instance7 = new $class(...$parameters);

$instance8 = new YourClass(...$parameters);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment