制作帮手的更好方法

时间:2015-06-23 04:17:44

标签: php function codeigniter-2

我有一个经常使用的方法。这是我的代码:

function dashboard_notification($arr_notification_data='')
{
    if($arr_notification_data == ''):
        return false;
    endif;

    $this->db->insert('message_notification', array(

    'dst_group'     => MESSAGE_DESTINATION_STUDENT_SPECIFIC,
    'dst_user_type' => USER_TYPE_STUDENT,
    'dst_user_id'   => $student_id,
    'alert_type'    => MESSAGE_ALERT_TYPE_EXCLAIMATION,
    'dt_expiry'     => date('Y-m-d H:i:s', time() + 14*24*60*60),
    'src_user_type' => USER_TYPE_ADMIN,
    'src_user_id'   => $this->curuser['admin_id'] ? $this->curuser['admin_id'] : 0,
    'msg_subject'   => 'New school name',
    'msg_content'   => 'Your school name was rejected, please update your school name again',
    'link_label'    => 'Update now',
    'link_url'      => 'http://url/profile/update/school/',
    'dt_added'      => timenow(),
    'dt_updated'    => timenow()
));
}

有没有更好的方法让这个功能成为帮手?由于这个功能有很多变数......

2 个答案:

答案 0 :(得分:0)

你可以改变多少你知道,我会做的一件事,我不会硬编码函数内部的值

$params = array(
'dst_group'     => MESSAGE_DESTINATION_STUDENT_SPECIFIC,
'dst_user_type' => USER_TYPE_STUDENT,
'dst_user_id'   => $student_id,
'alert_type'    => MESSAGE_ALERT_TYPE_EXCLAIMATION,
'dt_expiry'     => date('Y-m-d H:i:s', time() + 14*24*60*60),
'src_user_type' => USER_TYPE_ADMIN,
'src_user_id'   => $this->curuser['admin_id'] ? $this->curuser['admin_id'] : 0,
'msg_subject'   => 'New school name',
'msg_content'   => 'Your school name was rejected, please update your school name again',
'link_label'    => 'Update now',
'link_url'      => 'http://url/profile/update/school/',
'dt_added'      => timenow(),
'dt_updated'    => timenow()
);

然后我会将这些数据作为参数传递给函数,这样它的可重用性:

function dashboard_notification($arr_notification_data='', $params = array())
{
    if($arr_notification_data == ''):
        return false;
    endif;

    $this->db->insert('message_notification',$params);
}

答案 1 :(得分:0)

我会创建一个数据库适配器类,然后创建另一个扩展它的类。在这种情况下,它可以被称为StudentDatabase,它将包含您的dashboard_notification方法。

如果你真的想要一个助手,它看起来就像这样:

class StudentHelper {
    public static function dashboard_notification($curuser, $arr_notification_data='') {
        if($arr_notification_data == '') {
            return false;
        }

        // Your database object
        $db = new StudentDatabase();
        $db->insert('message_notification', array(
            'dst_group'     => MESSAGE_DESTINATION_STUDENT_SPECIFIC,
            'dst_user_type' => USER_TYPE_STUDENT,
            'dst_user_id'   => $curuser['student_id'],
            'alert_type'    => MESSAGE_ALERT_TYPE_EXCLAIMATION,
            'dt_expiry'     => date('Y-m-d H:i:s', time() + 14*24*60*60),
            'src_user_type' => USER_TYPE_ADMIN,
            'src_user_id'   => $curuser['admin_id'] ? $curuser['admin_id'] : 0,
            'msg_subject'   => 'New school name',
            'msg_content'   => 'Your school name was rejected, please update your school name again',
            'link_label'    => 'Update now',
            'link_url'      => 'http://url/profile/update/school/',
            'dt_added'      => timenow(),
            'dt_updated'    => timenow()
        ));
    }
}

然后包括该类并按如下方式调用它:

StudentHelper::dashboard_notification($this->curuser, <params>);