Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用两个不同的前提条件运行相同的函数?_Php_Laravel_Laravel 5.2 - Fatal编程技术网

Php 使用两个不同的前提条件运行相同的函数?

Php 使用两个不同的前提条件运行相同的函数?,php,laravel,laravel-5.2,Php,Laravel,Laravel 5.2,有没有办法不把这个弄干?其思想是在两个先决条件(=会话)下运行多个函数两次。我不想为函数(=doSomething和doAnything)编写前置条件(=会话变量更改) 有这样的东西会很好: runWithYears(&doAnything('world')); function runWithYears(func) { Session::put('year', 2016); // func should be called here

有没有办法不把这个弄干?其思想是在两个先决条件(=会话)下运行多个函数两次。我不想为函数(=doSomething和doAnything)编写前置条件(=会话变量更改)

有这样的东西会很好:

runWithYears(&doAnything('world'));

function runWithYears(func) {
            Session::put('year', 2016);
            // func should be called here
            exec(func)

            Session::put('year', 2015);
            // func should be called here and will return a different result
            exec(func)
}

谢谢

您的问题肯定不止这些,但实际上,您可以在设置变量后一起运行函数

    Session::put('year', 2016);
    doSomething('hello');
    doAnything('world');

    Session::put('year', 2015);
    doSomething('hello');
    doAnything('world');
编写代码如下:

    // run doSomething() and doAnything() 2x with different precondition (=Session var)

    Session::put('year', 2016);
    doSomething('hello');
    doAnything('world');

    Session::put('year', 2015);
    doSomething('hello');
    doAnything('world');

将对
exec
的调用替换为
call user func
应该具有非预期效果

虽然我个人更喜欢这两种方法

代码是

runWithYears('doAnything' , 'world');

function runWithYears(func, arg) {
            Session::put('year', 2016);
            // func should be called here
            call_user_func(func, arg);

            Session::put('year', 2015);
            // func should be called here and will return a different result
            call_user_func(func, arg);
}

虽然如果将其提取为两种方法会更易于维护和使用

但您要查找的是名为的匿名函数

匿名函数允许您将其声明为变量,这允许您将其作为任何其他变量传递给其他函数

示例-将匿名函数作为参数传递并执行它

$myFunction = function (){
echo "I'm anonymous";
}

function runWithYears(func){
$func(); // Calling the anonymous function
}

runWithYears(myFunction);
输出:我是匿名的


谢谢@Ante Gulin的帮助

这是我使用闭包的工作概念证明

new Sandbox();

class Sandbox {

    private $message;

    function __construct() {
        $this->first_method();
        $this->second_method();
    }

    private function first_method() {
        $test = function() {
            echo($this->message." peter");
        };

        $this->shout($test);

    }

    private function second_method() {
        $test = function() {
            echo($this->message." spiderman");
        };

        $this->shout($test);
    }


    private function shout($func) {
        $this->message = "hello";
        $func();

        $this->message = "cheerio";
        $func();
    }
}

您的问题对我来说非常不清楚。我不想为函数(=doSomething和doAnything)编写前提条件(=会话变量更改)。我还添加了一些伪代码。是的,但是doSomething()和doAnything()在我的类的不同方法中被调用。你知道如何将其封装/提取到一个方法中吗?是的,但是doSomething()和doAnything()在我的类的不同方法中被调用。有没有办法将其封装/提取到一个方法中费边5秒前编辑
new Sandbox();

class Sandbox {

    private $message;

    function __construct() {
        $this->first_method();
        $this->second_method();
    }

    private function first_method() {
        $test = function() {
            echo($this->message." peter");
        };

        $this->shout($test);

    }

    private function second_method() {
        $test = function() {
            echo($this->message." spiderman");
        };

        $this->shout($test);
    }


    private function shout($func) {
        $this->message = "hello";
        $func();

        $this->message = "cheerio";
        $func();
    }
}