how to run a php code in widget?

Try this with or without the return.

    $object_terms = wp_get_object_terms($GLOBALS['post']->ID, 'staff', array('fields' => 'all'));
    if ($object_terms) {
        echo '<p>' . '' . '' ;
        $res="";
        foreach ($object_terms as $term) {
            $res .= '<a href="' . esc_attr(get_term_link($term, $taxonomy)) . '" title="' . sprintf(__("View artiles of: %s"), $term->name) . '" ' . '>' . $term->name . '</a>, ';
        }
        echo rtrim($res,' ,').' ' . '</p>';
    }