[英]Returning of new $this with abstract classes
我发现我的代码有些麻烦,并且不明白它为什么会这样做。 有人能解释一下吗?
我们有:
abstract class AbstractThing
{
public function search(...)
{
$ret = false;
$data = $database->query(...);
foreach($data as $values)
{
$item = new $this;
$item->fill_with_values($values);
$ret []= $item;
}
return $ret;
}
}
它可以按预期工作,并在成功搜索时返回对象实例:
class Thing extends AbstractThing
{
// ...
}
$thing = new Thing;
$things = $thing->search(...); // Thing[] on success, false on failure
但是,如果我希望稍微缩短代码,它会破坏:
abstract class AbstractThing
{
public function search(...)
{
$ret = false;
$data = $database->query(...);
foreach($data as $values) {
$ret []= (new $this)->fill_with_values($values);
}
return $ret;
}
}
这返回布尔值为true。 为什么? 它适用于不是从抽象类继承的类。
代码做了两件事:
这会将$ item添加到“$ ret”数组中:
$item = new $this;
$item->fill_with_values($values);
$ret []= $item;
这会将返回的值“fill_with_values”添加到您的数组中:
$ret []= (new $this)->fill_with_values($values);
相当于上面的代码将是:
$item = new $this;
$return = $item->fill_with_values($values);
$ret []= $return;
如果我知道你的“fill_with_values”方法发生了什么,我可以告诉你为什么它是一个布尔值,但代码不会做同样的事情。 希望有道理。
当我们分配时:
$ret []= (new $this)->fill_with_values($values);
...我们没有设置$ret[] = (new $this)
。 相反,此语句将fill_with_values()
的返回值推送到数组中,因为它最后执行。
看起来你正在尝试实现类似于工厂方法模式的东西。 考虑一下:
abstract class AbstractThing
{
...
public static function fill($values)
{
$instance = new static;
$instance->fill_with_values($values);
return $instance;
}
}
然后我们可以在你的问题中实际完成你想要完成的事情:
$ret[] = static::fill($values);
这是有效的,因为fill()
的返回值是类的实例,而不是fill_with_values()
的返回值。 此上下文中的static
关键字使用后期静态绑定来解析执行代码的类的类型(在本例中为Thing
)而不是声明它的类,因此它通过继承来工作。 有关更多信息,请参阅此问题 。
好的,最后这是我自己的错误。 在某些时候,确实有可能从fill_with_values()函数返回TRUE。 对不起有问题,谢谢你的回答!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.