Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121789 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 7432 invoked from network); 23 Nov 2023 20:31:25 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 23 Nov 2023 20:31:25 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id D0B9F18003B for ; Thu, 23 Nov 2023 12:31:29 -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=-0.2 required=5.0 tests=BAYES_20,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, 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.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-ot1-f51.google.com (mail-ot1-f51.google.com [209.85.210.51]) (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 12:31:29 -0800 (PST) Received: by mail-ot1-f51.google.com with SMTP id 46e09a7af769-6d7f3fc69f4so685440a34.1 for ; Thu, 23 Nov 2023 12:31:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1700771484; x=1701376284; darn=lists.php.net; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=Q3HKQkujnDdBGIEFR68WQ83j/i0SdkhEbkzBX0icrEw=; b=mtXl3Q1mISViZmRl5VI0hgt2kqafU5EHh5yB3qzw4xfVbLdE8dGwwaUanGZowc8yJu w1cCEhB0ZJDOYGKxMdoWuEkuU7YiZ9G6m9ytX10y5nzon4BgH83ntIXvDmQji9JvTPUR 6OKWFg5oMF5oS/oWZpFndDwti0U5GIhaFNmCbSWrCoY/JMecsTVPfohQZdW2Dsdpq8xI +5IDVP4siSnJYad9nFcjNH/hrRiSWxtHV6cG80wOTd8xxp02X/iwzxT1fH1/TCL71Qyx 3hUk4c0D9xuvxgGcpSdISeMMUcro0ENBBXcaIRPfLF3gLNSYZFoTYmG20ZzlP2Y0Wc1J CueA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700771484; x=1701376284; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=Q3HKQkujnDdBGIEFR68WQ83j/i0SdkhEbkzBX0icrEw=; b=qj86Zkx+thyDRiW3G1scgM9xqzzrCbWWFo4Imt1NtqbBZgpEKEZ84h8i5gJMbb6RZM 81OhLwyUF7xgIDXnHjjK93Lzs0U843HLn823Vqaj5pixE37Bc67juSW8uwNeflkHsXTS 3kvqwgZ0XivxskNqS1Hwrt2LxSd65zqC952AgFvL6YxEVZ8dsfbi5qjwVVdA4ax09jVA x8Qo8MWVEMHa9XUN7KIiyl//IMZRfaHfiVcWaP8JHlgyxMMyecRMp/eIKHjdILt9Q9S0 UI4p9s3b3DT3OjUmqk86dKU/SseHI53RAJrIFXZ4Mz2F58ATr6KeI2cYWmfffKyuIr7g lQNA== X-Gm-Message-State: AOJu0Ywo/HxKUNhtYo4zIRn/G+i4p/uLd9YBSAEApa+AFqmKY5lgGDA6 yzFXGS/J7r4mbXJ/Z5D0HYL2JpLRuBfMfCTnRhY1vJobsyE/ow== X-Google-Smtp-Source: AGHT+IGOEx6DwKPZQje7Vq5Kf019ACXn5mmwz2XTBIduvxX29ILvuQB4Peo3hGlcSA9YRlwxBQFoyfwRYelK+L4vPuY= X-Received: by 2002:a05:6870:75ca:b0:1c8:b870:4e22 with SMTP id de10-20020a05687075ca00b001c8b8704e22mr509388oab.3.1700771483652; Thu, 23 Nov 2023 12:31:23 -0800 (PST) MIME-Version: 1.0 Date: Thu, 23 Nov 2023 21:31:09 +0100 Message-ID: To: internals Content-Type: text/plain; charset="UTF-8" Subject: [RFC][Discussion] Why can constructors violate LSP? From: landers.robert@gmail.com (Robert Landers) Hello Internals, As you may know, an inherited method cannot reduce the visibility of an overridden method. For example, this results in a fatal error during compilation: class P { public function hello($name = 'world') { echo "hello $name\n"; } } class C extends P { private function hello($name = 'world') { parent::hello($name); echo "goodbye $name\n"; } } However, we can make certain methods private anyway, namely, constructors (I haven't gone hunting for other built-in methods yet). This is perfectly allowed: class P { public function __construct($name = 'waldo') { echo "hello $name\n"; } } class C extends P { private function __construct($name = 'world') { parent::__construct($name); echo "goodbye $name\n"; } } To my somewhat trained eye, this appears to violate the Liskov Substitution Principle, for example, this now can have hidden errors: function create(P $class) { return new (get_class($class))(); } proven by: $c = (new ReflectionClass(C::class)) ->newInstanceWithoutConstructor(); create($c); Even though we thought we knew that the constructor was declared public. 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.3.x. I'm more than happy to implement it. Does anyone feel strongly about this one way or the other? Robert Landers Software Engineer Utrecht NL