The working portion of most diodes are encased in an plastic that protects it from light because light hitting a PN junction causes a current to flow and you don't want that noise in most circuits. A photo diode is encased in a plastic that allows light to pass into the PN junction, where it generates a current when the light reflected from the obstacle hits it.
That's the general idea, you can get more details in many books by looking up photodiode or phototransistor, or by searching online. For example wikipeda has a discussion on photodiodes.