0

Possible Duplicate:
PHP: How to chain method on a newly created object?

I started off with this code:

$page = new Page();

$page->replace_tags(...);

$page->output();

I changed the signature of replace_tags to allow method chaining, by returning $this. Why can I still not write it like this?

new Page()->replace_tags(...)->output();

Or this:

(new Page())->replace_tags(...)->output();
Community
  • 1
  • 1
Eric
  • 95,302
  • 53
  • 242
  • 374

2 Answers2

1

I think you may need to chain the functions on the class instance:

$page = new Page();

$page->replace_tags(...)->output();
Tomgrohl
  • 1,767
  • 10
  • 16
0

You need to assign the object to a reference first:

$obj = new Page();
$obj->replace_tags(...)->output();
prodigitalson
  • 60,050
  • 10
  • 100
  • 114