Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121790 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 11436 invoked from network); 23 Nov 2023 21:27:10 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 23 Nov 2023 21:27:10 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 225D0180031 for ; Thu, 23 Nov 2023 13:27:11 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-3.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-wr1-f47.google.com (mail-wr1-f47.google.com [209.85.221.47]) (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, 23 Nov 2023 13:27:10 -0800 (PST) Received: by mail-wr1-f47.google.com with SMTP id ffacd0b85a97d-332ca7f95e1so844489f8f.0 for ; Thu, 23 Nov 2023 13:27:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1700774824; x=1701379624; darn=lists.php.net; h=content-transfer-encoding:mime-version:message-id:references :in-reply-to:user-agent:subject:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=V7LNK8taLXgkNmauwFO22/bubiLjZDqZhz1S7mGugqQ=; b=NnjqKPTv8DhbVpvPVwHJxcEa8IYJc6qjviuUN3Toez81GS8+C2hIeLEOkchrRHolIT FWCFLNgDzGTnlXlKPibWqYz3W/9eMZVgZkJbEi75KqBb8CKD8/EB0by9jj7qHigoJQik PO1SVnRAJ20ctaNdsqlafG7fLKCDA8KDJcGfGeyoNEXZywf/XWq43XUJL3aH1r60IA0b Cc7iwD3yONfN8c+qeUCwMQwjXCMNbo4EEGRCuz0GgR87bkG8MtkCYdBnvEG3xLri7bB4 zTAkZXl0/kApRHJSRlf52/YZQgLLDjVaPpKE5pBY5HiapzS7331StCGDShiI+sL1czBU Cbcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700774824; x=1701379624; h=content-transfer-encoding:mime-version:message-id:references :in-reply-to:user-agent:subject:to:from:date:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=V7LNK8taLXgkNmauwFO22/bubiLjZDqZhz1S7mGugqQ=; b=iRIWTQDmepOKPpS4Dmr2CN+0lPMaQTDk0RQF9E2T9SW69dPfE5rima/w+MfUZ6dglH 6y0GJlOwAc7/TTlWGseHefG2PkwvlpCCeMuonZ/PhbTyfrnGWdAV1xZ+CYOYUX5/iS+d uSaFiSAoK09VV4GHHwL02Kocts/f85CEtdF50F0JY3Fe8HsQrCWHZxBH4VD8uqJBkaoe QIST8SJwcPNbxM0B4HWnVnv8DsexbG2eVVdfALBuBNhvkwvwH2B4IOYZnZ2MIQawAXSI 0ykB2bM5Ya92pI+7J7i3RO5X8yn6Bs3WeOBuUZpKZNb847XfIHrmmTmqQ3Ycl0Bm/Ujp YJzA== X-Gm-Message-State: AOJu0YwtII9e+xwjjLKlXm1+Z2Epa0FV/Mrv1q5W8jrLYn/6zQBoPFTU NHbWLHoxj7UoDDWc0bu5txU9juwCBOI= X-Google-Smtp-Source: AGHT+IGxeNHfisFj/D5hSia7H+fy5M6YlbneVKnKwc2uWbBSmY+X9zIqH+8vo9B9EjnH1UbeWcsfmA== X-Received: by 2002:adf:eec6:0:b0:332:c4f7:1f83 with SMTP id a6-20020adfeec6000000b00332c4f71f83mr543406wrp.7.1700774824407; Thu, 23 Nov 2023 13:27:04 -0800 (PST) Received: from [127.0.0.1] (cpc83311-brig21-2-0-cust191.3-3.cable.virginm.net. [86.20.40.192]) by smtp.gmail.com with ESMTPSA id x1-20020adfec01000000b003313e4dddecsm2624036wrn.108.2023.11.23.13.27.03 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 23 Nov 2023 13:27:04 -0800 (PST) Date: Thu, 23 Nov 2023 21:27:00 +0000 To: internals@lists.php.net User-Agent: K-9 Mail for Android In-Reply-To: References: Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] [RFC][Discussion] Why can constructors violate LSP? From: rowan.collins@gmail.com (Rowan Tommins) On 23 November 2023 20:31:09 GMT, Robert Landers wrote: >I'd like to propose an RFC to enforce the covariance of constructors >(just like is done for other methods), to take effect in PHP 9, with a >deprecation notice in 8=2E3=2Ex=2E There's a lot more than visibility that is enforced on normal methods, but= isn't on constructors=2E For instance this is also valid: class A { public function __construct(int $foo) {} } class B extends A { public function __construct(string $bar) {} } From=20a theoretical perspective, I think the argument is roughly that class= es aren't first-class citizens that you can pass around, so substitutabilit= y doesn't apply=2E You can't for instance write a function that explicitly = depends on "a class definition inheriting from A", like this: function foo(class $class) { $instance =3D new $class(42); } You can certainly simulate such code with some strings and maybe a bit of = reflection, but the language isn't going to make any guarantees about it=2E I did just think of a counter-example, though, which is that "new static($= param)" is allowed, even though there's no way to know if $param will be ac= cepted by subclasses=2E Maybe it shouldn't be allowed? From=20a practical point of view, it's often very useful to sub-class someth= ing and provide a constructor with a different signature=2E Maybe your subc= lass has additional dependencies; maybe it can hard-code or calculate some = of the inputs to the parent constructor for a special case, etc=2E A private constructor can be used in conjunction with static methods to si= mulate multiple named constructors (createFromString, createFromRequest, et= c)=2E Given the lack of other guarantees, there's no particular gain in pre= venting that just because the parent class has a public constructor=2E Regards, --=20 Rowan Tommins [IMSoP]