Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123324 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 qa.php.net (Postfix) with ESMTPS id 664131A009C for ; Thu, 16 May 2024 20:31:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1715891527; bh=Z44fkz4rPDlauq7xrCZeg45Kzdiiaf6dQAz2x9UHDQw=; h=From:To:Subject:Date:From; b=n8QvGAiUZA4g+dOuxQb7yRQ1GL47HDxIq0BCvPpHEmS1WsvL+waq+zz3MrQ9He7hH XOkRxiGW/U63ibSk0YTKCxt/K3aeS1F+/9eK/9nOYY7FzxBDev/CMANzt4s3ymINYP B2zgscXgYIaL+sMFrOqZWeno0uoBlmEeP0JeRFheYjoBrqWAxr+53NsNL0auJ2hp6p ooA7f7mNLh5nmQb6OjR3JctzwmNnw9C5EQF6YsWvqktvsNSstZLIrIzanvl5W7b7Qy N1zJgWgko8izS71+9Zmy8HiJKI8QMSbIj5voN5d+bnxC5PEKMxBG7JIlbY4bUtPTG6 H2CnyuEbCLygw== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id EB1B9180004 for ; Thu, 16 May 2024 20:32:05 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.1 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, HTML_MESSAGE,RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: Error (Cannot connect to unix socket '/var/run/clamav/clamd.ctl': connect: Connection refused) X-Envelope-From: Received: from mxe.seznam.cz (mxe.seznam.cz [77.75.78.34]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Thu, 16 May 2024 20:32:05 +0000 (UTC) Received: from email.seznam.cz by smtpc-mxe-584bcb9555-5kz2b (smtpc-mxe-584bcb9555-5kz2b [2a02:598:64:8a00::1000:52c]) id 2700f7c76ceb35312676f687; Thu, 16 May 2024 22:31:11 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=email.cz; s=szn20221014; t=1715891471; bh=3iZQ1NgUGNFU0T+fN3nTXWK68JpS/7l1/U1ASA265as=; h=Received:From:To:Subject:Date:Message-Id:Mime-Version:X-Mailer: Content-Type; b=qcsb6q8TOXkNxh5eh5XYzGfHjMzG5r1/vrGp+mpwPtFo+UqWDF/65s2YqB3Zc44gC hpmvYs3oevnj0HRBCEskQ5gyYJn1zED6JFYj1z8JUnRRlJSyD8VUbdCDYsEPeJA+Q0 ZJOJV/9CpKWMkZ0jR5qBJdBzooScrx91M2A6QIuM24u5MOKGjPsrhAnXs2tmKtmZNi DqfxDTnmYgmktMk4n906QydRP7UwZMqi4mlTcUM6mJU5X2bAPZKKImJGeqS4mveRrv 8FLcbd7JwJFSfwd0PYnM2/9VynHW6taaOtP4NkulYv0BD5EHHN3LeOt1ITqTLhz9Fa d3jcBVXB6I69w== Received: from cst2-175-77.cust.vodafone.cz (cst2-175-77.cust.vodafone.cz [31.30.175.77]) by email.seznam.cz (szn-UNKNOWN-unknown) with HTTP; Thu, 16 May 2024 22:31:10 +0200 (CEST) To: Subject: [PHP-DEV] [RFC] [Vote] Type Guards for Classes Date: Thu, 16 May 2024 22:31:10 +0200 (CEST) Message-ID: <3A6.1T4AB.9eXWvq4hof.1cHcqE@seznam.cz> Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net Mime-Version: 1.0 (szn-mime-2.1.37) X-Mailer: szn-UNKNOWN-unknown Content-Type: multipart/alternative; boundary="=_2d9a20c81d14ef03040b18bd=85575bf5-61f0-5f6c-96cd-881acad77884_=" From: PaTrOnY@email.cz (=?utf-8?q?Patrik_V=C3=A1clavek?=) --=_2d9a20c81d14ef03040b18bd=85575bf5-61f0-5f6c-96cd-881acad77884_= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Introduction ************* This RFC proposes a new feature in PHP: type guards for classes (or interfaces). This feature aims to simplify and standardize the process of = verifying that a variable is an instance of a specific class, enhancing co= de readability and reducing boilerplate code. Motivation ************* Currently, in PHP, to ensure that a variable is an instance of a specific = class, developers need to use the `instanceof` operator and manually throw= an exception if the check fails. This results in repetitive boilerplate co= de scattered throughout the codebase. A new syntax, `(ClassName) $variable`, = is proposed to streamline this process by performing an instanceof check and = throwing a `TypeError` if the variable is not an instance of the specified= class. Proposal *********** Introduce a new type guard syntax for classes: ```php (Foo) $variable; ``` This syntax will internally perform the following operations: 1. Check if `$variable` is an instance of `Foo`. 2. If the check fails, throw a `TypeError` with a message indicating the = expected and actual types. Example: Consider the following class definition: ```php class Foo { =C2=A0 =C2=A0// class definition } ``` To ensure a variable is an instance of `Foo`, instead of writing: ```php if (!$variable instanceof Foo) { =C2=A0 =C2=A0 throw new TypeError('Expected instance of Foo, got ' . getty= pe($ variable)); } ``` Developers can use the new type guard syntax: ```php (Foo) $variable; ``` Backward Compatibility *************************** This feature introduces new syntax and does not affect existing code. It i= s fully backward-compatible, as it does not modify or deprecate any existing= functionality. --=_2d9a20c81d14ef03040b18bd=85575bf5-61f0-5f6c-96cd-881acad77884_= Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable
Introduction
*************

=
This RFC proposes a new feature in PHP: type guards for classes (or i= nterfaces). This feature aims to simplify and standardize the process of v= erifying that a variable is an instance of a specific class, enhancing cod= e readability and reducing boilerplate code.

Moti= vation
*************

Currently, in = PHP, to ensure that a variable is an instance of a specific class, develop= ers need to use the `instanceof` operator and manually throw an exception = if the check fails. This results in repetitive boilerplate code scattered = throughout the codebase. A new syntax, `(ClassName) $variable`, is propose= d to streamline this process by performing an instanceof check and throwin= g a `TypeError` if the variable is not an instance of the specified class.=

Proposal
***********
Introduce a new type guard syntax for classes:

<= /div>
```php
(Foo) $variable;
```

This syntax will internally perform the following operations:

1. Check if `$variable` is an instance of `Foo`.
2. If the check fails, throw a `TypeError` with a message indicatin= g the expected and actual types.

Example:
Consider the following class definition:

```ph= p
class Foo {
   // class definition
}
```

To ensure a variable is an ins= tance of `Foo`, instead of writing:

```php
<= div>if (!$variable instanceof Foo) {
    throw new Typ= eError('Expected instance of Foo, got ' . gettype($variable));
}=
```

Developers can use the new type gu= ard syntax:

```php
(Foo) $variable;
```

Backward Compatibility
*****= **********************

This feature introduce= s new syntax and does not affect existing code. It is fully backward-compa= tible, as it does not modify or deprecate any existing functionality. --=_2d9a20c81d14ef03040b18bd=85575bf5-61f0-5f6c-96cd-881acad77884_=--