Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121804 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 60562 invoked from network); 24 Nov 2023 11:46:33 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 24 Nov 2023 11:46:33 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 394DA180054 for ; Fri, 24 Nov 2023 03:46:37 -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=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING, 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-f170.google.com (mail-il1-f170.google.com [209.85.166.170]) (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:46:36 -0800 (PST) Received: by mail-il1-f170.google.com with SMTP id e9e14a558f8ab-359d559766cso5983315ab.1 for ; Fri, 24 Nov 2023 03:46:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=itafroma.com; s=google; t=1700826391; x=1701431191; darn=lists.php.net; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=W4/6pQ+GVUw0d+bot0orcRDSd4Q2D4i0aCiT3BdCcvE=; b=C0l/uNljPOsEnI32TeZwvd/OnnnDbtqw9s6BwHeiSx8W3zRZrxBq4vkaNuIS3SrhEP oILu/pomXkpt5hFjoC52bLxOtFFeFiwVePr+8lOHZrBF/jtvOLnLQVb6o/EaQRSQbDJj IKijLBD3PXUx02zFogs+ZuORc9UVInmhk2X+E= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700826391; x=1701431191; h=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=W4/6pQ+GVUw0d+bot0orcRDSd4Q2D4i0aCiT3BdCcvE=; b=P9YOgOv4Pv/QiPItKyEvuITWfZfm3MKRgri7Xk3+fYcSaJ9bGlafGQuxiqj4kVfb7E M1Zp4t9ye4bTq9Jd9flNJmAiMe8S5I+6VYWVdDlrgPlGSCjajd4IT8RLqAlTY9heRfvP 9VRLZTk9ixZd05OG9+V6jie8VZ+46+6fLLnMlDaYJOCJ5I28eOOXYif1EiwCrXE/ra/Z BVCynZ6k5S5b8rjtJS9OPTl9Rsk7rPT3yypL1Q3ebcK4GgG5hmyedT9/sz3InB+5ksS/ vrQKYhNzYYFYqhDv0JQ5vCC009fkgBPrfiUhF7wydX+Wq6sPRIggKEzvSFrGrxvm8pf0 U5kA== X-Gm-Message-State: AOJu0YyC1luvohe1el87twGiseDZjxnO1hSWVnSJIvMWIb8dMlvTVVqw 7FvImLSl2eJh8Zt+qWCVKo0RW5gK+FhLv4YtgEUcTg== X-Google-Smtp-Source: AGHT+IER6Cs5UZtQojlariw7WEfP5JPHXWqy6G5JnwhzKEiaA3aZopGO/E3RuLAvclbebJLbZ5NcNARwQG3InZpKMB4= X-Received: by 2002:a05:6e02:1848:b0:35c:768e:7793 with SMTP id b8-20020a056e02184800b0035c768e7793mr80279ilv.15.1700826391308; Fri, 24 Nov 2023 03:46:31 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Fri, 24 Nov 2023 03:46:20 -0800 Message-ID: To: Robert Landers Cc: internals Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] [RFC][Discussion] Why can constructors violate LSP? From: mark@itafroma.com (Mark Trapp) On Thu, Nov 23, 2023 at 12:31 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 (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 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