douji2520 2015-07-03 07:49
浏览 83
已采纳

PHP范围子匿名函数在父函数中改变变量

I have the following code.

function up() {
    $runafterupdate = [];
    Schema::connection('exitproducts')->create('exitcontrol_carmanager_manager',  function($table)
                {
                    $table->engine = 'InnoDB';
                    $table->increments('id');
                    $table->string('code');
                    $runafterupdate['code'] = true;
                    $table->timestamps();
                });
            }
    if(isset($runafterupdate['code'])) {
        echo "it worked!";
    }
}

And i'm used to JavaScript where you can alter the values of the parent scope, but aparently php follows different rules. I've tried reading through http://php.net/manual/en/language.variables.scope.php but I really don't want to use globals.

Is there a way to alter the variables in the parent scope with php or is my only resort in this case a global variable?

  • 写回答

3条回答 默认 最新

  • duanpu2272 2015-07-03 07:55
    关注

    After some more digging... why do I always find the answers AFTER I post a question...

    Using the use clause on the function you can use the variables you declare there in the "child" scope. That this isnt highlighted in the scope documentation in php docs beats me.

    Extracted from Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?

    Extending the scope of variables into anonymous functions

    $foo = 'bar';
    
    $baz = function () use ($foo) {
        echo $foo;
    };
    
    $baz();
    

    After some fiddling I found i can't directly modify array variables. Any modifications stay in the function scope but dont lift over to the parent scope.

    I made a simple holder object with setters and getters to get it to work.

    function scop1() {
    /** simple class to hold vars **/
    class holder {
     public $held = [];
     /** setter **/
     function update($what, $with) {
            $this->held[$what] = $with;
        }
     /** getter **/
     function get($what) {
        if(isset($this->held[$what])) return $this->held[$what];
        else return null;
     }
    }
    $var = new holder();
    /** works **/
    $var->update('hello','bye');
    $x = function() use ($var) {
       /** modify parent scope **/
       $var->update('hello','hello');
    };
    /** call anomynous function **/
    $x();
    /** it should say hello hello in held **/
    var_dump($var);
    }
    scop1();
    

    live sample: http://sandbox.onlinephpfunctions.com/code/d7464356c0712f2606b0f70ab952be4d782374dc

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?