The deal will have an average annual value of $6.25 million.
"Obviously, I'm really excited. It's a huge day not only for me, but for my family and friends as well," said Sanheim in a statement. "To be drafted not only by Philly, but in Philly, and now to sign a long-term deal here, it means a lot. I love this city, love the fan base and I'm excited to turn this around and get things going. There's no better place to win than Philly."
Sanheim, 26, led Flyers defensemen in assists (24), was tied for first in points (31) and second in goals (7) and shots last season. He was selected by Philadelphia 17th overall in the 2014 NHL draft and made his NHL debut in 2017.
The Flyers open their 2022-23 regular season Thursday night vs. the New Jersey Devils.