I and curious why spears where phased out of military service as they where (in my opinion) one of the best weapons for melee combat as it had the reach, better against armor and cheap to make why did countries/military’s stop using spears
In many ways, they never did!
Most of my knowledge is focused on Central and Western Europe, so that's what I'll focus on here. As you rightfully point out, spears have a lot of advantages for your average infantryman. They're cheap, relatively easy to use, add reach, and--importantly--enable infantry to resist cavalry attack. Just as an aside, I am going to be speaking in very general terms here, and I am sure many of our resident medieval and early modern weapons and military folks will be ready to eviscerate me for doing so. For this, I beg their forgiveness, but to adress this question, speaking in general terms should suffice.
Now, onto your question. As Europe moved from what we would term the 'late medieval' to the 'early modern' period, armies continued to change. While mounted armored lancers were still an important part of the army, of increasing importance were pike blocks, large units of infantry armed with extremely long spears. These pike blocks were effectively impervious to cavalry attack from the front, and proved effective against infantry formations as well. Into this mix came personal gunpowder weapons of ever increasing utility. Armed with arquebuses and muskets, these soldiers were capable of laying down effective fire, but--due to slow rate of fire of their weapons--were vulnerable to a rapid cavalry assault. As such, infantry formations developed that combined pike wielding soldiers with gunpowder weapon armed soldiers, leading to the period of warfare commonly known as "pike and shot". Throughout the 16th and 17th centuries however, as gunpowder weapons became more and more effective, the ratios of "pike" to "shot" began to change. While in the earlier parts of period, you could generally expect a unit to have a ratio of 1:1 for pike to shot, later on you would see--proportionally--more shot than pike, as the firepower of units began to be capable of deciding battles in and of itself. However, the pikes remained important in terms of protecting the infantry from cavalry.
So what happened? Why did these pikes disappear? Well, quite simply, the became unnecessary. Why? The invention of the bayonet. Once infantry were able to reliably attach a bayonet to their musket, each soldier was effectively both pike and shot, and every musket could be a spear. Firearm equipped infantry could now stand on their own against attacking cavalry, without the need for units of pikemen to shelter behind. Of course, this took time to change, and some nations maintained pikemen until well into the 18th century. This has remained the case up until today, with even modern armies training in the use of bayonet tipped rifles, which are--for all intents and purposes--spears that you can also use to shoot people.
So, from a certain point of view, spears were never really phased out. They just changed form.
Hopefully this helps to answer your question, and feel free to ask any follow ups.