Skip to Content
Menu

once()

A memoization utility function for PHP so that certain things can occur only once.

PHP May 2, 2025

Usage

PHP
nebula()->once($unique_id, $callback, ...$args)

Parameters

$unique_id
(Required) (String) A unique string
Default: None

$callback
(Required) (Function) The function to trigger one time only
Default: None

...$args
(Optional) (Variadic) The parameters for the function
Default: None

Parameter Notes

Important! The order of the parameters for this PHP function is different than the order of parameters for the JavaScript once() function!

Request or provide clarification »

Examples

Do something only once

PHP
$this->once('this is an example', function(){
    echo 'This will output only once!';
});

Do something once and pass variables into the once function

PHP
$this->once('this is an example', function($foo, $bar){
    echo 'This will output only once: ' . $foo . ' and ' . $bar;
}, $foo, $bar); //Pass as many parameters as you'd like
Was this page helpful? Yes No


    A feedback message is required to submit this form.


    Please check that you have entered a valid email address.

    Enter your email address if you would like a response.

    Thank you for your feedback!

    Source File

    Located in /libs/Utilities/Utilities.php on line 42.

    No Hooks

    This function does not have any filters or actions available. Request one?
    PHP
            public function once($unique_id, $callback, ...$args){
                static $called = array();
    
                if ( !isset($called[$unique_id]) ){
                    $called[$unique_id] = true;
                    $callback(...$args);
                }
            }
    

    Override

    This function can not be short-circuited with an override filter. Request one?