构造可以返回某些东西吗? [英] Is it ok for a construct to return something?
问题描述
情况:
我有一个处理HTML表单,清理字段并验证的表单类。
I have a form class which handles HTML forms, cleans up fields and validates.
我正在考虑为不同的表单字段创建单独的类,例如:文本/文件/选择/等。
I'm thinking about creating separate classes for the different form fields, e.g.: text / file / select / etc.
我正在考虑使用此方法类似于以下内容:
The way I'm considering to use this is something like the following:
$form = new Form();
$form->element['fieldname'] = new HtmlTextField('length'=>3);
然而,有人在SO告诉我,类构造永远都不会返回任何东西。
However someone here on SO told me once that a class construct should never return anything.
在上述情况下,它将返回表单字段。
In the above case it would return the form field.
__ construct( )
永远不应该返回任何东西?
Is it correct that a __construct()
should never return anything?
推荐答案
更是如此:返回的任何内容都将被丢弃,并且被调用对象的新实例就是返回值。
Even more so: whatever you return is discarded, and a new instance of the called object is just the return.
这篇关于构造可以返回某些东西吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!