Environment as local namespaces

You can provide a default definition for \U and renew it when the environment starts.

\newcommand{\U}{default definition} % maybe issuing error
\newcommand{\Ua}{definition for enva}
\newcommand{\Ub}{definition for envb}
\newcommand{\Uc}{definition for envc}

\newenvironment{enva}
 {\renewcommand{\U}{\Ua}...}
 {...}
\newenvironment{envb}
 {\renewcommand{\U}{\Ub}...}
 {...}
\newenvironment{envc}
 {\renewcommand{\U}{\Uc}...}
 {...}

Since environments form groups, the redefinition will end its effect as soon as the environment ends.


enter image description here

\documentclass{article}
\newcommand\Ua{AAA}
\newcommand\Ub{BBB}
\newcommand\Uc{CCC}
\newcommand\U{} % Check that \U is not in use
\newenvironment{enva}{\let\U\Ua}{}
\newenvironment{envb}{\let\U\Ub}{}
\newenvironment{envc}{\let\U\Uc}{}
\begin{document}
\begin{enva}
  Some text, \U\ some other texts
\end{enva}

\begin{envb}
  Some text, \U\ some other texts
\end{envb}

\begin{envc}
  Some text, \U\ some other texts
\end{envc}
\end{document}

Maybe define with a variable that changes between environments.

\documentclass{scrartcl}

\usepackage{etoolbox}
\newrobustcmd\definevariable[2][]
  {\providecommand#2{\csuse{_\environmentvariable_\detokenize{#2}}}%
   \expandafter\newcommand\csname\detokenize{_#1_#2}\endcsname}

\definevariable[a]{\U}{whatever}
\definevariable[b]{\U}{whatever else}
\definevariable[c]{\U}[1]{\uppercase{#1}}

\newenvironment{enva}{\def\environmentvariable{a}}{}
\newenvironment{envb}{\def\environmentvariable{b}}{}
\newenvironment{envc}{\def\environmentvariable{c}}{}

\begin{document}

\begin{enva}
  Some text, \U\ some other texts
\end{enva}

\begin{envb}
  Some text, \U\ some other texts
\end{envb}

\begin{envc}
  Some text, \U{some} other texts
\end{envc}

\end{document}