How to copy material-name to all selected objects

Hallo!
I need a bit help:
When i run my script example, it says: material are not define!
How i must define the material when i want to copy the name of the material to all selected objects?


class Copy_Materialname_to_Object(bpy.types.Operator):
    """Copy Material-Name to Object-Name"""
    bl_idname = "tp_ops.copy_materialname_to_object"
    bl_label = "Copy Material-Name to Object"
    bl_options = {'REGISTER', 'UNDO'} 

    def execute(self, context):
        selected = bpy.context.selected_objects 

        for obj in selected:     

            #find material name ???
            #matname = bpy.context.active_object.active_material.name            
            #matname = mat.name
            
            #for obj in matname:  
            #if matname:  
                               
            #copy material name to all selected ???
            mat.name = obj.name 
            
        return{'FINISHED'}  


I know there is a way, i found it once and forgot where…
Thanks for any help!

This?


for object in context.selected_objects:
  if hasattr(object.active_material, 'name'):
    object.name = object.active_material.name

Or from the active object’s active material


for object in context.selected_objects:
    object.name = context.active_object.active_material.name