Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121806 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 64728 invoked from network); 24 Nov 2023 12:34:33 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 24 Nov 2023 12:34:33 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 45882180043 for ; Fri, 24 Nov 2023 04:34:21 -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.3 required=5.0 tests=BAYES_05,DKIM_INVALID, DKIM_SIGNED,DMARC_MISSING,HTML_MESSAGE,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail.yourprovider.de (mail.yourprovider.de [157.90.159.104]) (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, 24 Nov 2023 04:34:20 -0800 (PST) Received: from [192.168.178.30] (xdsl-89-0-70-174.nc.de [89.0.70.174]) (Authenticated sender: dennis@birkholz.biz) by mail.yourprovider.de (Postfix) with ESMTPSA id 0ABC9B56B8 for ; Fri, 24 Nov 2023 13:34:14 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=dennis.birkholz.biz; s=default; t=1700829254; bh=9JvyBKc+QPeskqO+EqgTttluMAct7MmEAGnVys8gQAE=; h=Date:From:Subject:To:References:In-Reply-To:From; b=HqTS04lCYbQGRzL2cdvXOroNZG5/cuw5UylxyemaQYf44FKbx1I0J52Fnhpltj5tk IxMbs7Tu/LZ6xEwOZU58okxLdO443rioJr9nMJKG3/hhk+nn+BM96k/9WlvirfvfF1 9Evcu99dD+Oc0N+o4zG9uU2j7Q30jQrJEu2r+/2Kq8aH904uUTUCBCG8tjVvAV7ZzE ZhCUzHAjCsqe54SbBUVi0oSL5tE941Tqn0dEmym7XHmEAOOiOinKbChGR0wfiCYDy5 6U/ZEFzautOvok8CZJ0hNUPvJZb+0DV3bayBHDliK4sz2OpqMVFRLADVD0wVO0fxsj 4adsqx4DVI2vA== Content-Type: multipart/alternative; boundary="------------bWzFPskZ3CG3FHkI5juJYPG1" Message-ID: <3b4c6996-87a8-44bc-9588-fd2ce4940c67@dennis.birkholz.biz> Date: Fri, 24 Nov 2023 13:34:14 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird To: internals@lists.php.net References: Content-Language: de-DE In-Reply-To: Subject: Re: [PHP-DEV] [RFC][Discussion] Why can constructors violate LSP? From: php@dennis.birkholz.biz (Dennis Birkholz) --------------bWzFPskZ3CG3FHkI5juJYPG1 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Hi Robert, Am 23.11.23 um 21:31 schrieb Robert Landers: > 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"; > } > } you can enforce a public constructor by using an interface: ``` interface X {     public function __construct (int $foo); } class A implements X {     public function __construct (int $foo) {         echo __CLASS__ . ": $foo\n";     } } class B extends A {     private function __construct (int $foo) {         echo __CLASS__ . ": $foo\n";     } } ``` Which will give you the following error: PHP Fatal error:  Access level to B::__construct() must be public (as in class X) in test.php on line 14 That way you put the definition of the constructor into the contract. Greets Dennis --------------bWzFPskZ3CG3FHkI5juJYPG1--