I've been using the following approach:
$foo_called = false;
function foo()
{
if($foo_called)
{
return;
}
$foo_called = true;
// do something.
}
I've been wondering if a better / different approaches existed.
Just for code clarity, I'd do something like this:
function foo()
{
static $foo_called = false;
if (!$foo_called) {
$foo_called = true;
// etc.
}
}
You could use a static variable:
function foo() {
static $foo_called = false;
if ($foo_called) return;
$foo_called = true;
// do something.
}
Look at the singleton pattern?
from the manual "The Singleton pattern applies to situations in which there needs to be a single instance of a class. The most common example of this is a database connection. Implementing this pattern allows a programmer to make this single instance easily accessible by many other objects."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With