Difference between walker class and basic php

If you are building a theme just for your own website you could easily achieve with some PHP loops what the – indeed, more difficult to understand – walker class achieves. After all, the class just walks through a tree-like structure and does nothing more. If you know exactly what you want your tree to look like, you can go at it directly with PHP.

Things become different when you are building something more generic, like a theme that might have child themes. Then you want flexibility for your users. For instance, take the walker_nav_menu class which is itself already an extension of the walker class. The advantage is not so much in the html it generates by default. The difference is in the filters it incorporates, such as nav_menu_css_class, which allows a user to change the classes attached to menu <li> items with a function in his child’s functions.php. For example:

add_filter('nav_menu_css_class' , 'wpse240664_nav_class' , 10 , 2);
function wpse240664_nav_class($classes, $item){
     if(is_single() && $item->title == 'About Us'){
             $classes[] = 'highlight-this';
     }
     return $classes;
}

With this function the child theme adds a ‘highlight-this’ class to the menu item ‘About Us’ on single pages. The alternative would have been to edit the menu template file (probably header.php). Just to modify one menu item you would have to duplicate the whole header.php in your child theme. When the parent theme’s header.php is updated you’ll have to check your child copy for necessary changes. All this you avoid if you use the walker class. More difficult to implement, perhaps, but it saves time for other users of your code.