开发者

Why is this not valid PHP? [duplicate]

开发者 https://www.devze.com 2023-03-23 11:29 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: PHP: How to chain method on a newly created object?
This question already has answers here: Closed 11 years ago.

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:

(n开发者_运维问答ew Page())->replace_tags(...)->output();


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

$page = new Page();

$page->replace_tags(...)->output();


You need to assign the object to a reference first:

$obj = new Page();
$obj->replace_tags(...)->output();
0

精彩评论

暂无评论...
验证码 换一张
取 消