PHP 函数将默认值设置为对象
函数(实际上是另一个类的构造函数)需要一个 class temp
对象作为参数。因此,我定义了 interface itemp
并包含 itemp $obj
作为函数参数。这很好,我必须将 class temp
对象传递给我的函数。但现在我想为此 item $obj
参数设置默认值。我怎样才能做到这一点?
还是不可能?
测试代码澄清:
interface itemp { public function get(); }
class temp implements itemp
{
private $_var;
public function __construct($var = NULL) { $this->_var = $var; }
public function get() { return $this->_var ; }
}
$defaultTempObj = new temp('Default');
function func1(itemp $obj)
{
print "Got: " . $obj->get() . " as argument.\n";
}
function func2(itemp $obj = $defaultTempObj) //error : unexpected T_VARIABLE
{
print "Got: " . $obj->get() . " as argument.\n";
}
$tempObj = new temp('foo');
func1($defaultTempObj); // Got: Default as argument.
func1($tempObj); // Got : foo as argument.
func1(); // "error : argument 1 must implement interface itemp (should print Default)"
//func2(); // Could not test as I can't define it
A function (actually the constructor of another class) needs an object of class temp
as argument. So I define interface itemp
and include itemp $obj
as the function argument. This is fine, and I must pass class temp
objects to my function. But now I want to set default value to this itemp $obj
argument. How can I accomplish this?
Or is it not possible?
The test code to clarify:
interface itemp { public function get(); }
class temp implements itemp
{
private $_var;
public function __construct($var = NULL) { $this->_var = $var; }
public function get() { return $this->_var ; }
}
$defaultTempObj = new temp('Default');
function func1(itemp $obj)
{
print "Got: " . $obj->get() . " as argument.\n";
}
function func2(itemp $obj = $defaultTempObj) //error : unexpected T_VARIABLE
{
print "Got: " . $obj->get() . " as argument.\n";
}
$tempObj = new temp('foo');
func1($defaultTempObj); // Got: Default as argument.
func1($tempObj); // Got : foo as argument.
func1(); // "error : argument 1 must implement interface itemp (should print Default)"
//func2(); // Could not test as I can't define it
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你不能。但你可以轻松做到这一点:
You can't. But you can easily do that:
PHP 8.1
从 PHP 8.1 开始,您可以将对象的新实例定义为函数参数的默认值,不会出现错误,但有一些限制。
文档:PHP RFC:初始化程序中的新增功能
PHP 8.1
Since PHP 8.1 you are able to define a new instance of an object as a default value of the function argument without error, however with some limitations.
Documentation: PHP RFC: New in initializers
Arnaud Le Blanc 的答案可能存在问题 是在某些情况下您可能希望允许
NULL
作为指定参数,例如您可能希望以不同方式处理以下内容:如果是这样,更好的解决方案是:
A possible problem with Arnaud Le Blanc's answer is that in some cases you might wish to allow
NULL
as a specified parameter, e.g. you might wish for the following to be handled differently:If so, a better solution would be:
从 PHP 5.5 开始,您可以简单地使用
::class
来传递一个类作为参数如下:Since PHP 5.5 you can simply use the
::class
to pass a class as a parameter as follow:例如,在这种情况下,您可以使用我的小型库 ValueResolver:
并且不要忘记使用名称空间 < code>use LapaLabs\ValueResolver\Resolver\ValueResolver;
还有类型转换的能力,例如,如果变量的值应该是
整数
,那么使用这个:检查文档了解更多示例
You could use my tiny library ValueResolver in this case, for example:
and don't forget to use namespace
use LapaLabs\ValueResolver\Resolver\ValueResolver;
There are also ability to typecasting, for example if your variable's value should be
integer
, so use this:Check the docs for more examples