XF 1.5 Cannot Redeclare Error

Gossamer

Active member
My current add-on is working great, until I tried to setup a cron job. The cron job is supposed to run through each user, check a specific field (stored as a serialized array) for a post that falls within a particular date.

However, the cron job is giving me this error:
Fatal error: Cannot redeclare expiredDate()

I'm using array_filter to find my expired dates in the array. The expiredDate function is called from array_filter and I'm thinking that's the cause of this error. I'm not sure how else to write this portion though.

Here is the php:
CronEntry
PHP:
class Goss_RoleplaySystem_CronEntry_ActivityCheck
{
/* Run a cron entry to delete expired posts for AC */
public static function deleteExpiredPosts()
{

$userModel = XenForo_Model::create('XenForo_Model_User');

/* Get all ac posts for all users */
$users = $userModel->getActivityPostsForAllUsers();

/* Cycle through users and check for expired posts */
foreach ($users as $user)
{
$activityPosts = $user['rp_messages_for_activity'];

if($activityPosts)
{
$activityPosts = unserialize($activityPosts);
$userModel->checkExpiredPosts($activityPosts);
}
}
}
}

Model
PHP:
public function checkExpiredPosts(array $activityPosts = array())
{

function expiredDate($date)
{
$timeframe = XenForo_Application::getOptions()->rps_ActivityCheck_Timeframe_LowActivity;

$cutOff = XenForo_Application::$time - $timeframe;

return ($date < $cutOff);
}

if($activityPosts)
{
/* Filter out expired posts */
$posts = array_filter($activityPosts, 'expiredDate');

/* Remove expired posts from array */
$currentPosts = array_diff($activityPosts, $posts);

/* return new array without expired posts */
return $currentPosts;
}
else
{
/* There are no posts */
return;
}
}

The checkExpiredPosts function does work fine when I call it on only a single user. Any ideas?
 
For the record, here's the fix (PHP 5.3+):

PHP:
public function checkExpiredPosts(array $activityPosts = array())
{
    if ($activityPosts)
    {
        // Shorthand
        $xfOptions = XenForo_Application::getOptions();

        /* Filter out expired posts */
        $posts = array_filter($activityPosts, function($date) use ($xfOptions) {
            $timeframe = $xfOptions->rps_ActivityCheck_Timeframe_LowActivity;

            $cutOff = XenForo_Application::$time - $timeframe;

            return ($date < $cutOff);
        });

        /* Remove expired posts from array */
        $currentPosts = array_diff($activityPosts, $posts);

        /* return new array without expired posts */
        return $currentPosts;
    }
    else
    {
        /* There are no posts */
        return;
    }
}


Fillip
 
Back
Top Bottom