简体   繁体   English

只能调用一次的函数

[英]Functions that can be called only once

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." 从手册“单例模式适用于需要单个类实例的情况。最常见的例子是数据库连接。实现这种模式允许程序员使这个单个实例很容易被其他许多人访问对象“。

I can't think of an equally simple one right away. 我不能马上想到一个同样简单的。 This is simple and reliable. 这很简单可靠。 I suppose you forgot the global there, but otherwise it should work well. 我想你忘记了那里的global ,但除此之外它应该运作良好。

It depends a lot of the context and the reason you want the function to be called only once. 它取决于很多上下文以及您希望仅调用一次函数的原因。

If your function is there to initialize something or start something, you're approach is fine. 如果您的功能是初始化某些东西或开始某些东西,那么您的方法很好。 An other thing you could do, if you are initializing something, is to test if what you want to initialize exist or no. 如果要初始化某些内容,您可以做的另一件事是测试您要初始化的内容是否存在。

function initSomething() {
    if (isSomethingInit()) return;

    // Init Something
}

If you are using those function as callback to something, you can use lambda declaration instead of declaring a function. 如果您使用这些函数作为回调函数,则可以使用lambda声明而不是声明函数。 This will ensure that your function are only called from certain places. 这将确保仅从某些地方调用您的函数。

function somethingWithCallback($callback) {
    $callback(5);
}

somethingWithCallback(function ($e) {echo $e;});

I know the question is old, but I like my solution and want to share it. 我知道这个问题已经过时了,但我喜欢我的解决方案并想分享它。

$triggered = false;
$trigger = function($callback)use(&$triggered){
    if(!$triggered){
        $callback();
            $triggered = true;
    }
};

foreach(['Hello','World', 'Wuhuu!'] as $value) {
    $value = ' ' . $value;

    $trigger(function()use(&$value){    // will only run once
        $value = trim($value);
    });

    echo $value;
}

If you are using SESSION, it may help you.如果您正在使用 SESSION,它可能对您有所帮助。

// PHP
function foo(){    
    
    if(isset($_SESSION['foo_was_called']))
    {
       return false;
    }
    
    $_SESSION['foo_was_called'] = true;
  
    // your code ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM