【问题标题】:PHP function to set the default value as an objectPHP函数将默认值设置为对象
【发布时间】:2011-10-27 06:09:15
【问题描述】:

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

【问题讨论】:

  • 在这种情况下你可以使用我的小库ValueResolver,请检查我的答案

标签: php object default-arguments


【解决方案1】:

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

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

【讨论】:

  • 感谢found this 搜索php方法默认值后,没想到我可以将对象与空值进行比较。
【解决方案2】:

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

func2();
func2(NULL);

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

function func2(itemp $obj = NULL)
{

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

  // ...

}

【讨论】:

  • 这有意义吗?唯一有效的参数值是nullitemp 的实现。 (或至少应该从 API 的角度来看)在为缺失值 (null) 传递标记时没有行为差异,并且根本没有值。我同意,如果不输入参数,这在某些情况下实际上很有用。
  • 如果您没有传递参数,那么您是故意这样做的,因此您可能希望它执行一些默认行为。如果您要传递 NULL 参数,那么它可能需要 itemp 实现,因此您可能希望它记录错误和/或引发异常。
  • 鬼鬼祟祟,这样你就给 php 引入了一些重载。我喜欢。不确定我是否会认可它,但谁在乎... :)
【解决方案3】:

由于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

【讨论】:

  • 谢谢!遗憾的是,它不能与类型提示一起使用:函数 func2(SomeInterface $class= SomeObject::class) 产生致命错误:“类类型参数的默认值只能为 NULL”(使用 PHP 7.2 测试)
【解决方案4】:

PHP 8.1 及更高版本

自 PHP 8.1 起,您将能够将对象的新实例定义为函数参数的默认值而不会出错,但有一些限制。

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

文档:PHP RFC: New in initializers

【讨论】:

    【解决方案5】:

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

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

    别忘了使用命名空间use LapaLabs\ValueResolver\Resolver\ValueResolver;

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

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

    查看docs 了解更多示例

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-31
      • 2020-07-22
      • 2020-11-24
      • 2015-05-25
      • 1970-01-01
      • 2012-11-19
      • 2010-10-28
      相关资源
      最近更新 更多