Asked  9 Months ago    Answers:  5   Viewed   44 times

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.

 Answers

49

Just for code clarity, I'd do something like this:

function foo()
{
    static $foo_called = false;
    if (!$foo_called) {
        $foo_called = true;
        // etc.
    }
}
Wednesday, March 31, 2021
 
The_Perfect_Username
answered 9 Months ago
15
function round_up($value, $precision=0) {
    $power = pow(10,$precision);
    return ceil($value*$power)/$power;
}
function round_down($value, $precision=0) {
    $power = pow(10,$precision);
    return floor($value*$power)/$power;
}
Saturday, May 29, 2021
 
RenegadeAndy
answered 7 Months ago
95

If by "won't be executed" you mean "will do nothing when called more than once", you can create a closure:

var something = (function() {
    var executed = false;
    return function() {
        if (!executed) {
            executed = true;
            // do something
        }
    };
})();

something(); // "do something" happens
something(); // nothing happens

In answer to a comment by @Vladloffe (now deleted): With a global variable, other code could reset the value of the "executed" flag (whatever name you pick for it). With a closure, other code has no way to do that, either accidentally or deliberately.

As other answers here point out, several libraries (such as Underscore and Ramda) have a little utility function (typically named once()

  • ) that accepts a function as an argument and returns another function that calls the supplied function exactly once, regardless of how many times the returned function is called. The returned function also caches the value first returned by the supplied function and returns that on subsequent calls.

    However, if you aren't using such a third-party library, but still want such a utility function (rather than the nonce solution I offered above), it's easy enough to implement. The nicest version I've seen is this one posted by David Walsh:

    function once(fn, context) { 
        var result;
        return function() { 
            if (fn) {
                result = fn.apply(context || this, arguments);
                fn = null;
            }
            return result;
        };
    }
    

    I would be inclined to change fn = null; to fn = context = null;. There's no reason for the closure to maintain a reference to context once fn has been called.

  • [*] Be aware, though, that other libraries, such as this Drupal extension to jQuery, may have a function named once() that does something quite different.

    Tuesday, June 1, 2021
     
    davidb
    answered 7 Months ago
    50

    Let me suggest you a little bit more elegant decision. First variant (without throwing an exception):

    public class Example {
    
        private Long id;
    
        // Constructors and other variables and methods deleted for clarity
    
        public long getId() {
            return id;
        }
    
        public void setId(long id) {
            this.id = this.id == null ? id : this.id;
        }
    
    }
    

    Second variant (with throwing an exception):

         public void setId(long id)  {
             this.id = this.id == null ? id : throw_();
         }
    
         public int throw_() {
             throw new RuntimeException("id is already set");
         }
    
    Sunday, August 1, 2021
     
    konstantin
    answered 4 Months ago
    22

    add these code into the top of your script

    @set_magic_quotes_runtime(false);
    ini_set('magic_quotes_runtime', 0);
    
    Wednesday, August 18, 2021
     
    Warrior
    answered 4 Months ago
    Only authorized users can answer the question. Please sign in first, or register a free account.
    Not the answer you're looking for? Browse other questions tagged :
     
    Share