duanfei8149 2016-05-21 13:42
浏览 101
已采纳

上面定义的回调函数未找到

I have this code snippet that is supposed to find the differences between two arrays of feed items:

protected function execute()
{
    $existingFeedItems = $feed->getItems();
    $newFeedItems = $feed->loadItems();

    function compareFeedItemIds($feedItem1, $feedItem2)
    {
        return $feedItem1->getFeedItemId() == $feedItem2->getFeedItemId() ? 0 : -1;
    }

    $feedItemsAdded = array_udiff($newFeedItems, $existingFeedItems, "compareFeedItemIds");
    $feedItemsRemoved = array_udiff($existingFeedItems, $newFeedItems, "compareFeedItemIds");
    $unchangedFeedItems = array_uintersect($newFeedItems, $existingFeedItems, "compareFeedItemIds");
}

This will throw the error:

Warning: array_udiff() expects parameter 3 to be a valid callback, function 'compareFeedItemIds' not found or invalid function name

Even though I have defined that function above. What is the reason for PHP throwing this error? I have to add I am executing this from an object's method context.

  • 写回答

2条回答 默认 最新

  • doudi5291 2016-05-21 14:16
    关注

    If your callback function is defined within a namespace, then you need to indicate that namespace when you make the udiff() call.

    $feedItemsAdded = array_udiff($newFeedItems, $existingFeedItems, "namespace\\compareFeedItemIds");
    

    Otherwise PHP will search for the callback function in the global namespace

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?