I've read in posts such as this and this that the momentum operator is not self-adjoint in the infinite square well because the geometric space is a bounded region of $\mathbb R$, for example $[0,a]$ for a well of width $a$. As such, it leads to weird stuff happening like momentum not being conserved.
What I don't understand is why the domain of the wave functions cannot be extended to $\mathbb R$ and have $\psi$ simply equal $0$ outside the well. That way, instead of integrating from $0$ to $a$, we can integrate from $-\infty$ to $+\infty$. Then $$ \langle \psi | \hat p \psi \rangle = \frac{\hbar}{i}\psi^*\psi \bigg\rvert_{-\infty}^{+\infty} + \int_{-\infty}^{+\infty} \left(\frac{\hbar}{i} \frac{\mathrm d \psi}{\mathrm dx} \right)^* \psi \; \mathrm dx = \langle \hat p \psi | \psi\rangle, $$ and $\hat p$ would still be self-adjoint. Also, does the boundary condition for the stationary states $\psi(0) = \psi(a) = 0$ not predicate on the assumption that $\psi = 0$ outside the well? If $\psi$ was not defined outside the well, $\psi$ does not have to be continuous at the walls of the well, so $\psi(0)$ and $\psi(a)$ could equal any value.