A former NYPD cop who became a police officer in Texas to escape the Big Apple’s dangerous streets was killed Tuesday in a shootout with a suspect, sources said.

David Hofer, a graduate of NYU who served in Manhattan’s Ninth Precinct for five years, responded to a report of shots fired at about 3 p.m. in the town of Euless outside Dallas, law enforcement sources said.

Hofer and other officers encountered an erratic gunman who immediately opened fire, fatally wounding the NYPD veteran. Cops fired back, killing the suspect.

“He was a wonderful child, a wonderful police officer,” his mother, Sofija Hofer, told The Post.

“He was working this very difficult [Manhattan] precinct, so he had a lot of traumatic experiences . . . He ­decided to go to a safer place.”

Mournful cops gathered at the Ninth Precinct station house on the Lower East Side, where Hofer was well liked.

“He was a friend of mine and I’ll miss him deeply,” a grieving former colleague said.

Hofer was engaged to be married. His fiancée, Marta Danylyk, was out of town when her betrothed was slain and was called home to be told the tragic news.

“He was about to get married,” his mother said. “He and Marta were planning their future.”

The bright officer was the son of European immigrants. His mother said he wanted to write a book about his experiences with the NYPD.

“I thought he might be a scientist, like his father, but he always wanted to be a policeman, ever since he was a little boy,” she said.

“He was so brave. He tried to find humor even in the worst situations.”