Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:129105 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by lists.php.net (Postfix) with ESMTPS id B8CB71A00BC for ; Thu, 6 Nov 2025 02:40:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1762396841; bh=wMYoZs1hOltNSrpd4XDCDCm37kdYhHmGkOVF04+DnDc=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=kqKWMzwfPU3g61geMrVcj8rPfcSvGUIn3mehHZeV1jwDnq27bfjto2HY+ATL++o1E DGQFQWgoRhbx4T+RnnK1BEN0o8dPr1Bjwiq6Erb3sVyl6lQETIkxe7odypTZHEXSu1 MovwWzEvY0DlMdVrAjyfrhv+CIygM6/tapFQ6702nI6uCIhKb+7RUx9X0cLpMNHoqy awdMjMT416B8WFZJLPxKZeRHJsKO0Ln8vYgpp6txdBjiUe1I8WZtxR7S3a7HRhyG7H HOMcerfvKyurGZzHpzUaWTKv0dE+T4OCIOyT4ZZ7H3cfHaITw1+xQQ4ekbF9NrfSkc RnPX4q6WQ93hw== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id B3940180041 for ; Thu, 6 Nov 2025 02:40:40 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.5 required=5.0 tests=BAYES_05,DKIM_SIGNED, DKIM_VALID,DMARC_MISSING,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2, SPF_HELO_NONE,SPF_NONE autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: No X-Envelope-From: Received: from mail-vs1-f53.google.com (mail-vs1-f53.google.com [209.85.217.53]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Thu, 6 Nov 2025 02:40:40 +0000 (UTC) Received: by mail-vs1-f53.google.com with SMTP id ada2fe7eead31-5d5fbfca7e2so367364137.0 for ; Wed, 05 Nov 2025 18:40:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=carthage-software.20230601.gappssmtp.com; s=20230601; t=1762396834; x=1763001634; darn=lists.php.net; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=eGmrChv4YsvlClgH5EVwjMM/e34Rw7sjcebR3MHIXgA=; b=m7tC0GqSHK+JuL5XjpA7hZj0akrrzZtzSHkeZ1GVhu2GuuLRJjI0QkkvjCCUqQW/Gm EBQMta7bKvEf1PjWTW3tVAsiyhRZgoM9WEcDlvfIeYqZ6U6x5SeWoxMmMpS5Wo67Xpnn z2Km+CN4EmkgnZyG6VwnPo/FHaNDlkKVqLeSSEwW8I7wUOzV6Bk3V8I5gc/YCnOoEEtz ix0RY6VfS6kpWRqNcmq4EPh8lNiXgmVbNfZSwBjwkNpj8T7wjT7nB47DfvoKe8mcyG5C vAkzZFpODfVgLdEHyGSOfMCvNB/fSxREn9Wm8ftC1FolRx2vEVfvkne0bTaf+wTaOe1H 3O+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762396834; x=1763001634; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=eGmrChv4YsvlClgH5EVwjMM/e34Rw7sjcebR3MHIXgA=; b=Lu0dveYm6Gy4WMJAQrMIdq9ddyGPDmBkG/RYt9HNgy8o4HvvGLMsbh4I+QuI8fsXIQ ah8nlQ22DEho9SYpetveKpaeX9oovxNoPZn3uSkursuD5c3Kywpjc9Z7tdYTDgkcyW2M +D8+ciWUXDEA/QrUXfJGpbyqw5P6Z8D8pCY5sX/fWUPV4XD5jOO15JGsxvuhXuEiMbna t7ruMl2EO9i3DOhA6b1RslYNRbnH+KET50HnZTujP8mXnzqThA63twPO4O/YbObUOaNv y5ihfmp2qguYet/oc9Bvx8NvwKF86DxUrLW1/46cMWlswdoJwigZlyMDEXOWxmpaHwlq K+Xw== X-Gm-Message-State: AOJu0YwbVCxcJtZSkZnMZXDsgY13jLjyaWrz3AukrFylSLCQ//Gy3oNY wIYzBJcP9wBtcrZvGym1Fp+bPz99VHFd10vMZKKnC+ExJJ3kuAgR53QKRXdSpOetp1efi9K509D nb0g4bZMBeQnWFk9OcE+Gjim/TSH413sjoJhR5WRLpMXp/hKIt8FCXJJs/ss7 X-Gm-Gg: ASbGncu4DNVxclEmvn/PcHidodKJL9zj5riSos12R5jxoR/ZXxlan8lGwsXWvfTXLka hEyRYIdCKb9ceju4eDGdt0UDsTzeLboztW6v981uXYC6e3ggNIBGS1EnC4T1QkPcXxbbDrOPaK4 rUmLLMnrcbTwnoRIxt1nLpFZgPjPWoGYQ1md6Ax4ZplKbCyTsyuN01EJlr34x/MUqDIXxrDT/l6 v5Sas5FNoPXHxp4BXRfmt6z7EsvZ7Hj3G0XDD4+gkMkUcphP0JnKik6FcrHq5Z5M1IZSiy+SnCH A1ZM5x/clZv8LVV2Ug== X-Google-Smtp-Source: AGHT+IFlmNvq/PCZ3xwx6g8VvNGeCPb5hSrSuI4cH9ZesjvdInh7B5RIMUEf1c2kpgX/8jhv7R4KqiM8QRoOLBHDqLs= X-Received: by 2002:a05:6102:370f:b0:5db:f897:59f0 with SMTP id ada2fe7eead31-5dd9fe51174mr650133137.11.1762396834497; Wed, 05 Nov 2025 18:40:34 -0800 (PST) Precedence: list list-help: list-unsubscribe: list-post: List-Id: x-ms-reactions: disallow MIME-Version: 1.0 References: In-Reply-To: Date: Thu, 6 Nov 2025 03:40:23 +0100 X-Gm-Features: AWmQ_bnBdqh_4lf_VhJ43o3GKWfsWYKcEIScEzeuCQBtOHAvjBZUu72kIfRiVp0 Message-ID: Subject: Re: [PHP-DEV] [RFC][Discussion] use construct (Block Scoping) To: Edmond Dantes Cc: internals@lists.php.net Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable From: azjezz@carthage.software (Seifeddine Gmati) On Wed, 5 Nov 2025 at 06:27, Edmond Dantes wrote: > > If PHP applies unset or __enter__/__exit__ depending on whether an > interface is implemented, it will introduce hidden behavior in the > code, making it harder for developers to understand what is happening. > Compare the two cases: > > ```php > // I know for sure that Scope implements the interface > // required to be used with "with" > with $scope =3D new Scope() {} > > // I have no idea whether the File class implements > // the required interface or not. It=E2=80=99s unclear what will happen i= n the end. > with $file =3D new File("...") {} > > ``` > > So, in Python you cannot use arbitrary objects in a with statement, > only those that implement the __enter__ and __exit__ contract. Hello Ed, Thank you for your feedback. Regarding your concern about the clarity when using a `use` statement with objects that may or may not implement a `Disposable` interface, it does not matter to the application developer whether a future `Disposable` interface is implemented or not. Consider this example: ```php // PHP Builtin: interface DisposableInterface { public function dispose(?Throwable $error): void; } // Library Code: interface DatabaseTransaction extends DisposableInterface { public function execute(string $q): void; } interface DatabaseConnection { public function beingTransaction(): DatabaseTransaction; } interface DatabasePool { public function getConnection(): DatabaseConnection; } // Application Code: function do_work(DatabasePool $pool): void { using ( $connection =3D $pool->getConnection(), ) { using ($transaction =3D $connection->beingTransaction()) { $transaction->execute('...'); sleep(10); // more work. $transaction->execute('...'); } sleep(10); // more work } sleep(10); // more work } ``` In this scenario, the library author might not implement `Disposable` for the `DatabaseConnection` because its internal handle is automatically closed on `__destruct`, so to them, `Disposable` adds no value. However, for the `DatabaseTransaction`, they do implement it, as it allows the transaction to commit or rollback based on the exit status. From the application developer's perspective, both are temporary resources that are "allocated" and will be disposed of after the scope. How they are disposed of is decided by the maintainer of that resource (in this example, a third-party library). They might feel `__destruct` is sufficient (e.g., for a socket to be closed), or they might feel the need for `Disposable` to perform a specific action based on whether the operation finished successfully. Thanks, Seifeddine