Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121803 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 58974 invoked from network); 24 Nov 2023 11:36:44 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 24 Nov 2023 11:36:44 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 001B5180034 for ; Fri, 24 Nov 2023 03:36:48 -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.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DMARC_MISSING,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,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-il1-f172.google.com (mail-il1-f172.google.com [209.85.166.172]) (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 03:36:48 -0800 (PST) Received: by mail-il1-f172.google.com with SMTP id e9e14a558f8ab-35b048479b2so5796245ab.1 for ; Fri, 24 Nov 2023 03:36:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=marktrapp.com; s=google; t=1700825803; x=1701430603; 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=8he/0eOBjx5+IDj/F7x5572gsqadhHSMAufJ3WF7TbI=; b=OPVwzzpd3Fm9DLjymr0ib+jWZ/GK3sCpHEfQ2VQ3BXYZxDHy3goyw8Sgr1YP9dwSDx oeGSb3RIQOGWxfzoPrhlwerK6rm9jaVHJ+6zJ+GiVI2aqYYP3wXuE5fNGffMO190l2p5 5F2GoaMBlnpdEqrnL+H0Pu9K2kdH9Q3nxc82w= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700825803; x=1701430603; 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=8he/0eOBjx5+IDj/F7x5572gsqadhHSMAufJ3WF7TbI=; b=FSxmezSidK4IuWgdTEkFvySjGmvh/Is03849AKEc5VVjhL8N93bzBrmCcFD2ShhPVR aqbRIgfyDOWL6eBkvhBclA4xo9fOoCqX3owD/76HcKGSuOrpU070TF21lYwKV49GstgS MdW+3QaAHELPaoGV3v4gzKbMh4dJZRsrXb7RHNhR0vJrqE9WzDCtFjf3bHzCwGNYD0xE XDat/s0rSNWJ8wv/bw2EmQz2EYEnnxBiAzJ73y3A68sOLGa8Hx/Cr046ljvwHd7WnTsw Oe9YSspDlXp1tMDznGyqbPziisryFOYTAnlcpp5tF3O33Bj1KJZYdEUZK1uMUAtwnpnM MTIg== X-Gm-Message-State: AOJu0YzpPwmpj01ZoqP/FbIF+JRoefBx4t/NXe8XpGo2ryqopT3nJPFe b0rAb/cxJAW2TOZ2+xF59skRoPqQZvlTwAP+jmLwMBXWXvPuguyFzzE= X-Google-Smtp-Source: AGHT+IG6z/mowWKYZ7WUcrIFMTGiF8+U8tavwbHNdi20tViEHFffdJPxCAj9nbch/AVwSlx8AWlcPjj1v0J4I8j/27U= X-Received: by 2002:a05:6e02:1ba4:b0:35a:a2d8:e20d with SMTP id n4-20020a056e021ba400b0035aa2d8e20dmr3293990ili.15.1700825802780; Fri, 24 Nov 2023 03:36:42 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Fri, 24 Nov 2023 03:36:32 -0800 Message-ID: To: Robert Landers 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: mark@marktrapp.com (Mark Trapp) On Thu, Nov 23, 2023 at 12:31=E2=80=AFPM Robert Landers wrote: > > 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 =3D 'world') { > echo "hello $name\n"; > } > } > > class C extends P { > private function hello($name =3D '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 =3D 'waldo') { > echo "hello $name\n"; > } > } > > class C extends P { > private function __construct($name =3D '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 =3D (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 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 to = provide different creators than their supertypes. In addition, omitting cre= ators makes it easy for a type to have multiple implementations, allows new= creators to be added later, and re ects common usage: for example, Java in= terfaces and virtual types provide no way for users to create objects of th= e type. (Creators is Liskov's word for constructors, which they later define in the paper.) Hope that helps. - Mark Trapp