Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:130112 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 1C4321A00BC for ; Fri, 20 Feb 2026 18:29:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1771612166; bh=xO95jQuMThMxq2h5DjOkmajuo0fHHl/jgBIH5vBTjbs=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=EmTUZKdU9M3L109HFmp/CWyyNhZ+kj1kvG1R1oOf/zd50GxruHhn0CvihyKVyi3ON I1TvbHBwDh4d6RJZJotOzzhrRfhsz4D0+ZizWmsphVzInZHHjWDzS8oP5VakBBl5c1 7we4eiYRbIbyKBL3bVZSRAKM7bG3lYRrdm/yxJkZM6trp5l/aDdgbPqF6pvPeLTET/ zgLjAZC7bu9iHw2d/JS+9hCHFTXzisGia8AdUt0WaVNuwmKEJwJibZwD5cHWUfQNJE BIeAYJA2y/IOiVpna2iZTnrK9jYtRBOITeLyM4SZs0L4XAN7HHHJbXiS1nSqm72jwd t48FXTSO2lbGg== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 8A1151801D6 for ; Fri, 20 Feb 2026 18:29:25 +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.6 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_50, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS, FREEMAIL_FROM,HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: No X-Envelope-From: Received: from mail-ot1-f54.google.com (mail-ot1-f54.google.com [209.85.210.54]) (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 ; Fri, 20 Feb 2026 18:29:25 +0000 (UTC) Received: by mail-ot1-f54.google.com with SMTP id 46e09a7af769-7d4bc9e48bbso901406a34.2 for ; Fri, 20 Feb 2026 10:29:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1771612159; cv=none; d=google.com; s=arc-20240605; b=gicF97Xu9Zj3BsvlaeDO4MqL5Fkt4MLoOuAwm0H6BSTQfLxrPdpFi52Wz7fMsgK5f8 E6bvr28gtPfx8fmI3obmrBOeNWOEYfBTQr8EayfWeD1ZDnLdKcokDAxAtmdI5fXF6aCd Mrp7C029KRqrFNsYKGecfjcX4qVe979zM2nNLIO64Fd4dhP3hcqvCuB+IvqXVh90rfl0 R0VgP6Cyis3W4Zgx48LMHJUjEXptN9r/8i56U+VhXnmF8D5CMJO7nkpqqakIVuVZvsTc jaeP0JGqNqLAudO10FioD8lPY0LgrN1NCADW5Ep+jXF8PFsDOI/9is4ALQYDnSEAVivB lKHg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:dkim-signature; bh=oup+pwZU+Nno3IFVaV9y5h0ZyyrBMG9bELFNM+56goo=; fh=83Ag74npZ6yo1r+WqlOkfpmfkhUar+jailw0cOZ/kBQ=; b=SkMu9qSboM/HyBovOOXdtLhZKHZoPR9IbE5pbkRuZhDiH1iyHWiYimIHI8EAyT3yPB CfBMCpT+TTVe1yW8WIeDfbyMJ1pnc93QPefOPJlcxDceJQmqZLZe+MksM/0twU6wCmr1 zm7do54Lyj3CNUIygaou+j9bD4qmRTHvL6U+bD7fsYrtemXGj2uT0ooHEIIEhqKl3+8R DxQ6397oowTbafJhL2ncVb09J+gDFfhkKGiLn524RBicXB5XsHlKCcRYoUtj96RgEdCf R9ClNgk+lDmrig2ywG+lCN/rJzfxKrO4PL4HNQrvBujXz08TTvRS7o+OudT+3cvDJz/H GxqA==; darn=lists.php.net ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1771612159; x=1772216959; darn=lists.php.net; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=oup+pwZU+Nno3IFVaV9y5h0ZyyrBMG9bELFNM+56goo=; b=c0gczGZsO8TQNUYhGYpwU6plyKO67VhtBj2IoeQxGZfj1VkSNnB6r4G52w3UnVQ4GO 8oSDO50+AeYsLMc/ltjbYs1IBfU61UgRqqOMSnRtx4xIwjlcAORsjmq0eqr1Kqceh2eM UDxfpvwshWsaFt3ZZeDSCC6jc7Dwgb+E2hpwf6GtAIOg2G6no61rlbAhuQ004Jq7cQ3V FBO+cEeLSIDEAuCWhysKaR5N1QJCBN/DDjWr5iS0SyVMs7xlX2s/tTcCE5smsvP+Bp/M ba5aTLT3+4/smUhOlGwDVGRiEcCL1wSjvRdnTSj+vUXtltcKV/c8YOALlWWWRBVGfNs9 6ocw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771612159; x=1772216959; h=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=oup+pwZU+Nno3IFVaV9y5h0ZyyrBMG9bELFNM+56goo=; b=YEjjON+qNDHkdCxL7OZU15fEFocv7xsGVoSy6KywIHGGG10NW5alIOKll1MvZBKRs7 DvKHIK8xCRNPitTcAVng1ugfgoSg0llJrg7+S6f4fWvsJtTJ5dsZ8Egw9r8X3i4rPjQo JcVS7xOSipanV9j2X8qWlfLRDNSffHBJmYXsfQNmGgDWzovXy0ZFMJmCGBLGw6guekgl B1KkZQ/F4w6r6ImjOquaKREMyvcgHykNGAdKYyJY4xBD2BGryemyxvfCelXXuqSa+WCl a/V1mZ3uRDA6YBtz1AUuEO+V4e6tRDyfyuLjwsMZl0GXSt5+zJaEEUoH82AA/QeGwqwA HDdQ== X-Forwarded-Encrypted: i=1; AJvYcCW+pCpHZafazgZi3p0UJ/6w2QcOMSV5gH3rpgAS/ZQum/rPe3fJFo0FS0+R/l6CKT85NOQzk8ozC0w=@lists.php.net X-Gm-Message-State: AOJu0Yx1aMr1ftZl/tpM9hvSyykxEFnINNgxwy6P8WSNq67VSnV/ENQG pTOTDHPmpXZhBvYTjKUgJLCDXWwJY2iOLvl6I6QrgRzFNYKiuRdbvMs8duJThIwgXw3mkbSCGY/ j3v/hNkBFr6utQmYbO2pT3clhcvrJutTsvg== X-Gm-Gg: AZuq6aLIsscG1ilcFN3aq8eBNL9ZL2D5PkZQ0hluAhCmKpq0Uss71PqZVmcOgzdi85u L0ijtEj4slR5Ir7NmsbCqaLmBADd9sm5clxH8wQxEODfjI6HphGiPpyG/LjdTWnh+Wfp1+lIhEB hCs+s+zf/7CiheylY8CaeI3UK8CQOv/0Go5PehgWkfoAPzxJRmEhW6MyxscASwjIL3JmSWHG9Fv XlAOt7aAn/CgqnyQA2JOG9FFU/q06NgvEa+aNKj12RUjzQbcpGZYhU/hgf+mmIdfU3kzqqToaGM 4criwSecQcYkSJbGBxbRKVYFkx6juTfG3Bdz9rg5ncpiVUHXuWHuZXsuF/GHZn8KzFawGw== X-Received: by 2002:a05:6820:4489:b0:679:a4fe:f01b with SMTP id 006d021491bc7-679c4097955mr431658eaf.0.1771612159495; Fri, 20 Feb 2026 10:29:19 -0800 (PST) Precedence: list list-help: list-unsubscribe: list-post: List-Id: x-ms-reactions: disallow MIME-Version: 1.0 References: <1f25d77e-224b-40d6-bf19-18dfdfc9de54@rwec.co.uk> <453bc043-b4ee-423e-9561-55510f5c20a7@rwec.co.uk> <6412E222-9B0F-4139-A6F9-7D0C686197BF@rwec.co.uk> In-Reply-To: Date: Sat, 21 Feb 2026 01:29:07 +0700 X-Gm-Features: AaiRm53zLO4cZeliLzho8xxNdySZvhllXI4g23iFFml_rnp6z5hy9i95gK76z1c Message-ID: Subject: Re: [PHP-DEV] [IDEA for RFC] let the "new" operator fail when the __construct() function returns a value. To: Mirco Babin Cc: "Rowan Tommins [IMSoP]" , PHP internals Content-Type: multipart/alternative; boundary="00000000000072546c064b4599a9" From: pierre.php@gmail.com (Pierre Joye) --00000000000072546c064b4599a9 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hello Mirco, On Fri, Feb 20, 2026, 11:48=E2=80=AFPM Mirco Babin = wrote: > Op vr 20 feb 2026 om 08:43 schreef Rowan Tommins [IMSoP] < > imsop.php@rwec.co.uk>: > > > > On 19 February 2026 20:24:56 GMT, Mirco Babin > wrote: > > > > > > >The Php scripting aspect makes it difficult in general > > >for untyped projects to fail early. > > > > > > This is a completely irrelevant statement. We're not talking about > > failing early "in general", we're talking about a compile-time check > > that PHP already makes in other contexts. > > Failing early is not always possible. The compiler can not always infere > the correct return type. E.g. this example will succeed to compile: > > ```php > class UnableToInfereReturnType > { > private function TruelyUnknown() : mixed > { > $a =3D time(); > if ($a % 2 =3D=3D=3D 0) { > return ['important']; > } > } > > private function ActAsVoid() : void > { > return $this->TruelyUnknown(); > } > > // Lets act as if the return type is implicitly void > public function __construct() // : void > { > return $this->ActAsVoid(); > } > } > > $it =3D new UnableToInfereReturnType(); > > // Runtime error, not a compile error. > // Fatal error: A void method must not return a value in /in/MJCjX on lin= e > 15 > > ``` > Both can, and should, have errors before runtime. This is easily detected at compile time. Whether or not the return is used in a branch, it is seen before runtime, obviously. In a context of __construct, disallow any return value can be detected as well. Any return statement with anything used with it can be rejected (and should). It is documented as void, we could "fix" it with a bug fix. Also, about this whole discussion , that __ construct, can still be called as a normal method. Some relics of php 4-5 migration path and then work around serialization or hydration challenges. Most maintained projects out there moved away from these hacks and rely on new reliable solution for these challenges, like doctrine or symfony serializer. If anything, this whole cleanup could be done right, disallow returning value and finally make __construct what it is designed and made for, a constructor. Not a normal method. __ Pierre @pierrejoye | http://www.libgd.org > --00000000000072546c064b4599a9 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hello Mirco,




On Fri, Feb 20, 2026, 11:48=E2=80=AFPM Mirco Babin <mirco.babin@gmail.com> wrote:
Op vr 20 feb 2026 om 08:43 schreef Rowan T= ommins [IMSoP] <imsop.php@rwec.co.uk>:
>
> On 19 February 2026 20:24:56 GMT, Mirco Babin <mirco.babin@gmail= .com> wrote:
>
>
> >The Php scripting aspect makes it difficult in general
> >for untyped projects to fail early.
>
>
> This is a completely irrelevant statement. We're not talking about=
> failing early "in general", we're talking about a compil= e-time check
> that PHP already makes in other contexts.

Failing early is not always possible. The compiler can not always infere the correct return type. E.g. this example will succeed to compile:

```php
class UnableToInfereReturnType
{
=C2=A0 =C2=A0 private function TruelyUnknown() : mixed
=C2=A0 =C2=A0 {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 $a =3D time();
=C2=A0 =C2=A0 =C2=A0 =C2=A0 if ($a % 2 =3D=3D=3D 0) {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 return ['important'];
=C2=A0 =C2=A0 =C2=A0 =C2=A0 }
=C2=A0 =C2=A0 }

=C2=A0 =C2=A0 private function ActAsVoid() : void
=C2=A0 =C2=A0 {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 return $this->TruelyUnknown();
=C2=A0 =C2=A0 }

=C2=A0 =C2=A0 // Lets act as if the return type is implicitly void
=C2=A0 =C2=A0 public function __construct() // : void
=C2=A0 =C2=A0 {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 return $this->ActAsVoid();
=C2=A0 =C2=A0 }
}

$it =3D new UnableToInfereReturnType();

// Runtime error, not a compile error.
// Fatal error: A void method must not return a value in /in/MJCjX on line = 15

```

Both can, and should, have errors before runtime.=C2=A0 This is easily d= etected at compile time. Whether or not the return <some value> is us= ed in a branch, it is seen before runtime, obviously.=C2=A0

In a context of __construct, disallow= any return value can be detected as well. Any return statement with anythi= ng used with it can be rejected (and should).=C2=A0
=
It is documented as void, we could "fix&qu= ot; it with a bug fix.

A= lso, about this whole discussion , that __ construct, can still be called a= s a normal method.=C2=A0 Some relics of php 4-5 migration path and then wor= k around serialization or hydration challenges.

=
Most=C2=A0 maintained projects out there moved away= from these hacks and rely on new=C2=A0 reliable solution for these challen= ges,=C2=A0 like doctrine or symfony serializer.

=

If anything,=C2=A0 this= whole cleanup could be done right, disallow returning value and finally ma= ke __construct what it is designed and made for, a constructor. Not a norma= l method.=C2=A0

=C2=A0
__
Pierre

@pierrejoye | http://www.libgd.org
--00000000000072546c064b4599a9--