PHP 函数将默认值设置为对象

发布于 2024-11-30 01:14:15 字数 1028 浏览 0 评论 0原文

函数(实际上是另一个类的构造函数)需要一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

女皇必胜 2024-12-07 01:14:15

你不能。但你可以轻松做到这一点:

function func2(itemp $obj = null)
    if ($obj === null) {
        $obj = new temp('Default');
    }
    // ....
}

You can't. But you can easily do that:

function func2(itemp $obj = null)
    if ($obj === null) {
        $obj = new temp('Default');
    }
    // ....
}
不美如何 2024-12-07 01:14:15

PHP 8.1

从 PHP 8.1 开始,您可以将对象的新实例定义为函数参数的默认值,不会出现错误,但有一些限制。

function someFunction(Item $obj = new Item('Default'))
{
    ...
}

文档: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.

function someFunction(Item $obj = new Item('Default'))
{
    ...
}

Documentation: PHP RFC: New in initializers

神经大条 2024-12-07 01:14:15

Arnaud Le Blanc 的答案可能存在问题 是在某些情况下您可能希望允许 NULL 作为指定参数,例如您可能希望以不同方式处理以下内容:

func2();
func2(NULL);

如果是这样,更好的解决方案是:

function func2(itemp $obj = NULL)
{

  if (0 === func_num_args())
  {
    $obj = new temp('Default');
  }

  // ...

}

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:

func2();
func2(NULL);

If so, a better solution would be:

function func2(itemp $obj = NULL)
{

  if (0 === func_num_args())
  {
    $obj = new temp('Default');
  }

  // ...

}
菩提树下叶撕阳。 2024-12-07 01:14:15

PHP 5.5 开始,您可以简单地使用 ::class 来传递一个类作为参数如下:

function func2($class = SomeObject::class) {
    $object = new $class;
}

func2(); // Will create an instantiation of SomeObject class
func2(AnotherObject::class); // Will create an instantiation of the passed class

Since PHP 5.5 you can simply use the ::class to pass a class as a parameter as follow:

function func2($class = SomeObject::class) {
    $object = new $class;
}

func2(); // Will create an instantiation of SomeObject class
func2(AnotherObject::class); // Will create an instantiation of the passed class
那支青花 2024-12-07 01:14:15

例如,在这种情况下,您可以使用我的小型库 ValueResolver

function func2(itemp $obj = null)
    $obj = ValueResolver::resolve($obj, new temp('Default'));
    // ....
}

并且不要忘记使用名称空间 < code>use LapaLabs\ValueResolver\Resolver\ValueResolver;

还有类型转换的能力,例如,如果变量的值应该是整数,那么使用这个:

$id = ValueResolver::toInteger('6 apples', 1); // returns 6
$id = ValueResolver::toInteger('There are no apples', 1); // returns 1 (used default value)

检查文档了解更多示例

You could use my tiny library ValueResolver in this case, for example:

function func2(itemp $obj = null)
    $obj = ValueResolver::resolve($obj, new temp('Default'));
    // ....
}

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:

$id = ValueResolver::toInteger('6 apples', 1); // returns 6
$id = ValueResolver::toInteger('There are no apples', 1); // returns 1 (used default value)

Check the docs for more examples

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文