How to prevent nested function calls?

First question, please forgive me, if I do something wrong or is it a duplicate, I could not find another question like that.

I am writing a very basic translator function i18n($string1)that $string1asks for an index when asked. If the string matches something in the index, it returns a translation $string2. If it $string1does not exist in the index, a new record is created in the index, which the user can later write translated $string2into.

The problem I want to try to solve is that a careless developer (s) might accidentally call i18n(i18n($string1)). The way this happens is that the internal call finds $string1in the index, returns $string2, and then passes it again to the function that it is trying to find $string2in the index. This will not work because it $string2is a translation and it will prompt the user to translate the translation ... which does not help.

I believe that I want, in case the function is called nested inside itself, it returns a string $string1. So it doesn't matter how many times I call i18ninside of myself, it always translates the string $string1.

What is the best way to do this? I considered using a static or global variable $isCalledor singleton, but I was told in the past that these solutions are almost certainly evil and maybe something better.

Thanks in advance!

+3
source share
1 answer

You were told correctly.

You can parse debug_backtrace (), but it really is time and resources. Also, this does not require the overhead of the translation function.

If a developer calls a translation function with a string already translated, the developers are responsible for fixing this problem with non-software.

- PHP_CodeSniffer , . , .

+3

All Articles