Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121805 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 62441 invoked from network); 24 Nov 2023 12:04:12 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 24 Nov 2023 12:04:12 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id DF790180047 for ; Fri, 24 Nov 2023 04:04:16 -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=-1.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_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_PASS,URI_DOTEDU autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-oa1-f51.google.com (mail-oa1-f51.google.com [209.85.160.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 ; Fri, 24 Nov 2023 04:04:16 -0800 (PST) Received: by mail-oa1-f51.google.com with SMTP id 586e51a60fabf-1f9ec5dac5bso380481fac.3 for ; Fri, 24 Nov 2023 04:04:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1700827450; x=1701432250; darn=lists.php.net; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=OvZQpPR6d4KJ+4Qi7bYi4BUK9ohF3vsGZKyK6GUOodw=; b=hMxkhGA6b5nz60XBMhroJvCNvpYQT85akBAKog2lRMTY+0M00CXOSuINSZRhSvYaHG PH6VeRs56jUgNOoJyqSRJKbpmJ/Iwxk4JNInb582M211RfAs8RmDs1JrN2VK6no3W75N DhnqVOufcNbZ6lO2lqS2NVIq5j/tA1CL2Tu6sh0HoDyKInaqxDrNWWFNxFNXCEyoH4ka SiXqp097bJgcOis0vQkZWAAcPUPyvULp2xkvFs+HdmXhC3rED0zUG5Iivxou8oksW6Dd ZDdx/65uWou+dUo+thZBkB4x5UbO1kfgktNSi68HFCSMXk0fjKVyLFK3zWkVAWpyDXhY sf+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700827450; x=1701432250; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=OvZQpPR6d4KJ+4Qi7bYi4BUK9ohF3vsGZKyK6GUOodw=; b=tONGakuMzim//3u3p6k75mOC0grp/rl+Re3qAwg8HSm5pRdSx6/NurG8Tq7GBH7DRi +3DGM6HpWS6L8OwyrDdrnfylPGChx7fJM2q1SEUmS4HobbKjJuVzM9Ov/z7bVt6xW7F1 NYXyNynVCmGsxB5BOdrXUyq5PvfFzKtjteFQ3zXbupGDI5on/uK4BnVW+JRlIYNTCHzr aSYld2/ocFKbOzl0rLBChT9njavhcYQYeAyD/K7sBCuhn7aiLYOLQJgK2TCTUIo+LdCF fLL7wYDIoNZVU843WJmmPRWqZs+emm8u5G9Y+s3KENIVa1TiRRa7XWCZFR2ZS5See70s I3xw== X-Gm-Message-State: AOJu0Yzq7LknS8yQAeC1jtzz55nUVO48hymo/D/vRNB/uVpzvZco4DmA rOiMcc25bMOT0cEX+DuS7v02oRcifTI+RxtpPA0= X-Google-Smtp-Source: AGHT+IFeArX+tgGpgscOhP9ToqV+tNlbKbrEZB+CLTFGBg28jUYTdHjxYtTfqFHN9DvPz2yBND1f6YxXZ3OqC4CdE6I= X-Received: by 2002:a05:6870:2181:b0:1f9:8f39:43a8 with SMTP id l1-20020a056870218100b001f98f3943a8mr2966192oae.53.1700827450613; Fri, 24 Nov 2023 04:04:10 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Fri, 24 Nov 2023 13:03:59 +0100 Message-ID: To: Mark Trapp Cc: internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] [RFC][Discussion] Why can constructors violate LSP? From: landers.robert@gmail.com (Robert Landers) On Fri, Nov 24, 2023 at 12:46=E2=80=AFPM Mark Trapp wro= te: > > On Thu, Nov 23, 2023 at 12:31 Robert Landers w= rote: > > > > 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 =3D3D 'world') { > > echo "hello $name\n"; > > } > > } > > > > class C extends P { > > private function hello($name =3D3D '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 =3D3D 'waldo') { > > echo "hello $name\n"; > > } > > } > > > > class C extends P { > > private function __construct($name =3D3D '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 =3D3D (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 > > (Apologies: resending because I used the wrong email address to send > to the list) > > Hi Robert, > > Liskov and Wing explicitly exempted constructors in their original > paper describing the principle: > http://reports-archive.adm.cs.cmu.edu/anon/1999/CMU-CS-99-156.pdf > > > 4.1 Type Specifications > > > > A type specification includes the following information: > > > > - The type's name > > - A description of the type's value space > > - A definition of the type's invariant and history properties > > - For each of the type's methods: > > - Its name; > > - Its signature including signaled exceptions; > > - Its behavior in terms of pre-conditions and post-conditions. > > > > Note that the creators are missing. Omitting creators allows subtypes t= o =3D > provide different creators than their supertypes. In addition, omitting c= re=3D > ators makes it easy for a type to have multiple implementations, allows n= ew=3D > creators to be added later, and re ects common usage: for example, Java = in=3D > terfaces and virtual types provide no way for users to create objects of = th=3D > e type. > > (Creators is Liskov's word for constructors, which they later define > in the paper.) > > Hope that helps. > > - Mark Trapp Thanks Mark, That does help quite a bit! Thanks! I might think they are wrong, but if so, I'll probably need to write a paper and prove it before I come back. :D Cheers! Rob