如何在闭包里面调用外部函数和使用$this调用函数



方法一(不推荐)

            $that = $this;
            $form->saving(function (Form $form) use ($that) {
                // 判断是否是新增操作
                if ($form->isCreating()) {
                    $res = $that->getHttpMobile($form->mobile);
                    dd($res);
                }

            });

方法二 (推荐)
只需要把要调用的函数改为 static

            $form->saving(function (Form $form) {
                // 判断是否是新增操作
                if ($form->isCreating()) {
                    $res = self::getHttpMobile($form->mobile);
                    dd($res);
                    $form->operator = $res['data']['operator'];
                    $form->qcellcore = $res['data']['area'];
                    $form->price = "120";

                }

            });

本文由 来鹏飞 创作,采用 知识共享署名 3.0,可自由转载、引用,但需署名作者且注明文章出处。

还不快抢沙发

添加新评论