0

Im trying to do this with PHP

$query->select('username')->from('users')->execute();

I would rather have it like this

$query->select('username')->from('users');

is there a magic function that allows a function like execute to be started at the end of a call and return the contents?

SinisterGlitch
  • 195
  • 3
  • 25

1 Answers1

3

No, there's no such thing. PHP doesn't know what something like this "ends". Method chaining is just shorthand for:

$a = $foo->a();
$b = $a->b();
$c = $b->c();

There's no way to determine when this chain of calls will "end", and it's not desirable to bind behaviour to such syntactic sugar.

What if you want to do something like this?

$a = $foo->a();

if ($bar) {
    $a->bar();
}

$a->b()->c();

if ($baz) {
    $a->baz();
}

$a->execute();
deceze
  • 510,633
  • 85
  • 743
  • 889