Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:115120 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 99297 invoked from network); 24 Jun 2021 15:14:44 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 24 Jun 2021 15:14:44 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 066BC1804D8 for ; Thu, 24 Jun 2021 08:33:32 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.6 required=5.0 tests=BAYES_00,BODY_8BITS, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-lf1-f46.google.com (mail-lf1-f46.google.com [209.85.167.46]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Thu, 24 Jun 2021 08:33:31 -0700 (PDT) Received: by mail-lf1-f46.google.com with SMTP id i13so10996811lfc.7 for ; Thu, 24 Jun 2021 08:33:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=AJfGe80j/HnZxyPmApBLmdniSyLj3+9D/+WjEhAsngA=; b=S+b0ZP1F9FSL8RVo4O30ixsah21pyFaGMT+MG26I/km/HWeqy3ApR/wCu7m9w1mFTN Ur+RzBk6Eenwnm4AE5JEzRgvj3Tm9wQnHnQDf46xnV6lJz1kolTnpLmfTfpxJBQMyuE7 r2P6Pm22oyDH8ZLM/JuCSNMMYAiG8Iu56g++HcJcNvzPdRDmoH4OW7rmoFGfZie7z+sH ZNwT1UqOOfnFb0FtXnldBZDMRpZXLbkyrHDUGnukG7CARlji/KXiijc5S7FXuYr1qlNI fJJi3+5/CjfOQywrPiRF+sR84OODzxuKwtvzywzElA8H1uDhmjfcT+nGOu9zy0yDSmUQ UB7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=AJfGe80j/HnZxyPmApBLmdniSyLj3+9D/+WjEhAsngA=; b=WtEhouLZGZLf7ov91mjAp2VFjdpoO9EEgK/ZG5Qcrev4R4Y3++OL7RrXPK8LjF5v5R lB3xM8TKOm4BvNy9cX3KHDiL/2SfY0c3msElh+jfXFenoTp3W0BpGIv7LKi5jBhYFlGs I+ZHbm5tBM8aZXqv1GIQpD6xwshHo5cY8KtFImQ/2FFVz3TsBHUxuAqipmMZ2JXMnPRp +Q1KFyh+S0fVas69UkFQPYNZcBoFV97HULHjqSpA4OuFFMy1F1u79YOxgyq4SIkl9dVJ 1cmSZy2XOlnetbkmo6enY3vL5Lb9RBwRdC/gZiBYOMqWkDl20T+QkUudJ/9d7rYyLl6+ Xxtw== X-Gm-Message-State: AOAM530LQPXbzQvjTXfp6//rfj/XjAu2GNZD3ZEWdlzJdKBBEhmjeP+N Ehmv7wDCDt4IV0yVp7A4Ev0zdMEf0Ea/D/DOZfc= X-Google-Smtp-Source: ABdhPJy5+MJaRU7I4gBfL7W07X5mKHcuonHfwXwKdC5EqzRMGW1aZjVD1IpEI3gu9TP/EdXlvFNoZAK15phekLrdmvU= X-Received: by 2002:a05:6512:1392:: with SMTP id p18mr4317669lfa.294.1624548809894; Thu, 24 Jun 2021 08:33:29 -0700 (PDT) MIME-Version: 1.0 References: <81EDE991-60C8-4DF8-9D1E-1B2BAE144829@gmail.com> In-Reply-To: <81EDE991-60C8-4DF8-9D1E-1B2BAE144829@gmail.com> Date: Thu, 24 Jun 2021 18:33:19 +0300 Message-ID: To: Claude Pache Cc: internals@lists.php.net Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Checking that Closure is static From: elisdnru@gmail.com (Dmitry Eliseev) =D1=87=D1=82, 24 =D0=B8=D1=8E=D0=BD. 2021 =D0=B3. =D0=B2 17:38, Claude Pach= e : > Le 18 juin 2021 =C3=A0 16:35, =D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9 = =D0=95=D0=BB=D0=B8=D1=81=D0=B5=D0=B5=D0=B2 a =C3=A9cri= t : > > Hello! When I use a static anonymous function as an HTTP-action in > some frameworks like: > > $app =3D new App(); > $app->get('/', static function () { > return new Response('Hello!); > }) > > I get a warning "Cannot bind an instance to a static closure" in every > $closure->bind($container) call inside of the framework. > > But right now I cannot prevent this warning because there is no native > way to check that closure is static before calling the bind method. I > can only suppress the warning with @ or set/restore_error_handler > pair. > > So I propose to add a method like Closure::isStatic (or > Closure::canBeBound, etc.) for ability of manual checking it before > bind call: > > if (!$closure->isStatic()) { > $closure->bind($object); > } > > The method can contain simple statement: > > return func->common.fn_flags & ZEND_ACC_STATIC; > > It will be helpful if somebody needs to support dynamic and static > callables both without warnings. > > I sent Pull Request https://github.com/php/php-src/pull/7193 with a > simple implementation. > > -- Best, Dmitry > > > I think that the Warning should be removed entirely, and the `bindTo()` m= ethod should be a no-op when applied to a static closure. > > That the closure is implemented as static, or as non-static without actua= lly using `$this`, is an implementation detail that the user of the closure= doesn=E2=80=99t need to be aware. > > As a precedent, it is perfectly legal to call a static class method on an= instance of that class: https://3v4l.org/5QKsN Yes, removing this warning can be a better solution because bindTo() already returns FALSE on failure. But the bindTo() method is more general by design. It can be used for binding/unbinding/associating closures and it triggers different warnings in each case. So without warnings we lose information about the concrete reason for failure. And in quiet no-op mode without catching and logging these warnings we can miss real problems with incorrect binding in source code of our projects. Throwing an exception is a strict solution, but it is a BC-breaking change for the next major release.