It seems your aim is to make theorem numbers subordinate to the highest (non-\part) sectioning level defined in the respective document class -- \chapter for the book class, and \section for article. Therefore, I suggest to test not for the name of the loaded class, but for the availability of the \chapter command. If this command is undefined (or its meaning is \relax), number theorems per section, else, per chapter. This method will also work for, e.g., the report class and the KOMA-Script classes.
\documentclass{book}
\usepackage{etoolbox}
\ifundef{\chapter}{%
\newtheorem{teo}{Teorema}[section]% if \chapter not defined
\typeout{Numbering theorems per section.}%
}{%
\newtheorem{teo}{Teorema}[chapter]% if \chapter defined
\typeout{Numbering theorems per chapter.}%
}
\begin{document}
\begin{teo}
A theorem.
\end{teo}
\end{document}
.sty). Solutions here had LaTeX fail. It worked using\@ifclassloadedalong with bits taken from: conditionals - Test if a package (or package option) is loaded - TeX - LaTeX Stack Exchange – Stéphane Gourichon Mar 05 '14 at 08:57