Why does this need to be done programmatically? WordPress’s menu editor lets you manually add a CSS class. If you aren’t seeing this option then look in the top, right of your screen for the “Screen Options” and click it so that it expands. Then check “CSS Classes”.