账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    easywechat代码分析call_user_func
    89
    0
    $payment = EasyWeChat::payment();
    try {
        $payment->handlePaidNotify(function ($message, $fail) {
            $fail('xxx');
        });
    } catch (EasyWeChat\Kernel\Exceptions\Exception $e) {
        Log::warning('WeChatPay Notify Exception',
            ['code' => $e->getCode(), 'message' => $e->getMessage()]
        );
    }

    这里为啥$fail能这样用,追了下源码

    namespace EasyWeChat\Payment\Notify;
    
    use Closure;
    
    class Paid extends Handler
    {
        /**
         * @param \Closure $closure
         *
         * @return \Symfony\Component\HttpFoundation\Response
         *
         * @throws \EasyWeChat\Kernel\Exceptions\Exception
         */
        public function handle(Closure $closure)
        {
            $this->strict(
                \call_user_func($closure, $this->getMessage(), [$this, 'fail'])
            );
    
            return $this->toResponse();
        }
    }
    //toresponese
        public function toResponse(): Response
        {
            $base = [
                'return_code' => is_null($this->fail) ? static::SUCCESS : static::FAIL,
                'return_msg' => $this->fail,
            ];
    
            $attributes = array_merge($base, $this->attributes);
    
            if ($this->sign) {
                $attributes['sign'] = Support\generate_sign($attributes, $this->app->getKey());
            }
    
            return new Response(XML::build($attributes));
        }
        //strict
            protected function strict($result)
        {
            if (true !== $result && is_null($this->fail)) {
                $this->fail(strval($result));
            }
        }
    

    上边来看$fail实际上传入的是[$this,'fail'],那为啥能$fail(‘’);
    是我理解错文档了还是代码分析错了

    2
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 11 元积分
        全部回答
    • 0
    • 我在y城收快递 普通会员 1楼

      EasyWeChat是一个微信开放平台,提供了API来实现微信客户端的API功能。call_user_func函数在EasyWeChat中是一个常用的函数,它用于调用用户提供的函数。

      下面是一个简单的call_user_func函数的代码示例:

      php function call_user_func($func, $arg) { // 如果arg为null,则直接调用func函数 if ($arg === null) { $func(); } else { // 使用call_user_func_array函数将arg数组转换为调用函数的参数 $func_array = array($func, $arg); // 使用call_user_func函数调用调用函数的参数 call_user_func_array($func_array, $arg_array); } }

      在这个函数中,我们首先检查arg是否为null。如果arg为null,我们直接调用func函数。否则,我们将arg数组转换为调用函数的参数,然后调用func函数。这样,我们就可以在调用函数的参数中包含任何需要的参数。

      需要注意的是,call_user_func函数的参数必须是数组。如果传入的参数不是数组,或者数组中没有我们需要的元素,那么call_user_func函数将返回false。此外,call_user_func函数返回的结果也是一个数组,但是它包含了原始参数以及调用函数的返回值。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部