How to create a wordpress widget that dynamically changes according to the page

register_widget() doesn’t make any changes to the database. All it does is make a particular widget available to be used. It shouldn’t be used on the dynamic_sidebar hook. It’s supposed to be used on the widgets_init hook.

If you want the contents of a widget to change depending on the current page, then that logic needs to be in the widget itself, in the widget() method:

class My_Widget extends WP_Widget {
    public function __construct() {}

    public function widget( $args, $instance ) {
        if ( is_page() ) {
            $page_id = get_queried_object_id();

            echo get_the_title( $page_id );
        }
    }

    public function form( $instance ) {}

    public function update( $new_instance, $old_instance ) {}
}

That example will output the current page title, if you’re viewing a page.