Narrated Nafi' ibn AbdulHarith: I went out with the (Messenger of Allah ﷺ until I entered a garden, he said: Keep on closing the door. The door was then closed. I then said: Who is there ? He then narrated the rest of the tradition. Abu Dawud said: That is to say, the tradition of Abu Musa al-Ash'ari. In this version he said: "He then knocked at the door".
The Prophet Muhammad was not only teaching us about safety but also about respect for others and their property. By knocking on the door before entering, he showed respect for whoever may have been inside by letting them know someone was coming in first rather than just barging in without warning. This teaches us an important lesson on respecting other people's privacy as well as our own safety by taking precautionary measures such as closing doors behind us or knocking before entering a room or area where someone else may already be present.