php函数如何设置默认值为对象?

时间:2021-04-18 17:11:17

A function(actually constructor of another class) needs an object of class temp as argument. So I define interface itemp and include itemp $obj as function argument. This is fine, I must pass class temp objects to my function. But now I want to set default value to this itemp $obj argument. How to accomplish this? Or is it not possible?
I will put the test code to clarify:

函数(实际上是另一个类的构造函数)需要一个类temp的对象作为参数。所以我定义了接口itemp并将itemp $ obj包含为函数参数。这很好,我必须将类临时对象传递给我的函数。但是现在我想为这个itemp $ 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

4 个解决方案

#1


26  

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

你不能。但你可以很容易地做到这一点:

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

#2


3  

A possible problem with arnaud576875'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:

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

func2();
func2(NULL);

If so a better solution would be:

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

function func2(itemp $obj = NULL)
{

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

  // ...

}

#3


0  

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

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

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

and don't forget to use namespace use LapaLabs\ValueResolver\Resolver\ValueResolver;

并且不要忘记使用命名空间使用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

查看文档以获取更多示例

#4


0  

since php 5.5 you can simply use the ::class to pass a class as a parameter as follow:

从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

#1


26  

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

你不能。但你可以很容易地做到这一点:

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

#2


3  

A possible problem with arnaud576875'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:

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

func2();
func2(NULL);

If so a better solution would be:

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

function func2(itemp $obj = NULL)
{

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

  // ...

}

#3


0  

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

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

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

and don't forget to use namespace use LapaLabs\ValueResolver\Resolver\ValueResolver;

并且不要忘记使用命名空间使用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

查看文档以获取更多示例

#4


0  

since php 5.5 you can simply use the ::class to pass a class as a parameter as follow:

从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