Lightly grease a 1.5 litre/2 1/2 pt/6 cup fairly shallow ovenproof dish with about one third of the butter.
Melt the remainder in a frying pan (skillet) and fry (saute) the onions and garlic for 2 minutes to soften.
Put a layer of about a quarter of the potatoes in the base of the dish.
Spread about a third of the onion and butter mixture over, then top with two slices of the ham.
Season lightly and sprinkle with about a quarter of the cheese.
Repeat the layers and finish with a layer of potatoes, topped with the remaining cheese.
Whisk the eggs and milk or cream together and pour over.
Bake in a preheated oven at 180Â°C/350Â°F/gas mark 4 for 1 1/4 hours or until the potatoes are tender and the top is golden brown.