【发布时间】:2013-09-13 14:07:26
【问题描述】:
我有以下代码,它尝试按产品的创建日期对数组进行排序:
private function sortProductsByDate(Product $a, Product $b)
{
if ($a->getCreated() == $b->getCreated()) {
return 0;
}
return ($a->getCreated() < $b->getCreated()) ? -1 : 1;
}
/**
* Get the most 4 recent items
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getMostRecentItems()
{
$userMostRecentItems = array();
$products = $this->getProducts();
usort($products, "sortProductsByDate");
foreach ($this->getProducts() as $product) {
ladybug_dump($product->getCreated());
}
$mostRecentItems = $this->products;
return $this->isLocked;
}
为什么这会给我这个错误:
Warning: usort() expects parameter 1 to be array, object given
想法?
【问题讨论】:
-
您在上一行缺少
;。 -
@MichaelBerkowski 现在是另一个错误
-
是的,您将处于回调的错误上下文中。您read the
usort()docs 看到调用类成员函数作为其回调的部分了吗?