Home > Backend Development > PHP Tutorial > php链式调用如果在new实体之后马上调用方法

php链式调用如果在new实体之后马上调用方法

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 20:20:49
Original
1959 people have browsed it

网上查了一下,php要实现链式需要自己编码,并且好像只能是方法后接着调用方法。
但是我要求实现的是类似这种链式,是new了实体之后接着调用方法:

<code>new MyClass()->setData('1');
</code>
Copy after login
Copy after login

好像就无法实现,即使在构造函数里return $this,但是IDE还是提示有错。

回复内容:

网上查了一下,php要实现链式需要自己编码,并且好像只能是方法后接着调用方法。
但是我要求实现的是类似这种链式,是new了实体之后接着调用方法:

<code>new MyClass()->setData('1');
</code>
Copy after login
Copy after login

好像就无法实现,即使在构造函数里return $this,但是IDE还是提示有错。

<code>(new MyClass())->setData('1');</code>
Copy after login
Related labels:
php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template