Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:128426 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by lists.php.net (Postfix) with ESMTPS id 1A28A1A00BC for ; Thu, 7 Aug 2025 22:49:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1754606883; bh=xBWlq2KaH/aqSd+RtwH8h9vbzPQM9u6inTQQpE6EyhU=; h=Date:Subject:To:References:From:In-Reply-To:From; b=TCFElhIjfXgT82IzQBSShbihIVhJ7JkD9f2W3prPT1HJC+4FPbuA0v9LTvO5lcoo4 P5zmTtpkeZ90OjCItxew58bf4iSuero1/KOIifsklmV5xzgacXFAsq5NjpwG809HGu EzGRhzNlK0WKDd/RKhwphhoPHfKVoSMLbeXWQ1mC4ZnoOxMeh4HguigVA9+TyUBAHl zZt0lbUC4pgfnDzOWua9KR3dnam568X4RcjPj7TPKzBmeMEFxXSUhvYdqPt/vz30Hj g5POUraDATEDRTaHi0bagJSv1GQbiRfgAdm0eN3JocxLa+j0r9EaEiwzJKwUchKeh6 oBCsn3agIpiJg== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id C5169180054 for ; Thu, 7 Aug 2025 22:48:00 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=0.8 required=5.0 tests=BAYES_50,DMARC_MISSING, SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: No X-Envelope-From: Received: from gavin.smtp.mailx.hosts.net.nz (gavin.smtp.mailx.hosts.net.nz [43.245.52.167]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (prime256v1) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Thu, 7 Aug 2025 22:48:00 +0000 (UTC) Received: from 122-57-11-211-adsl.sparkbb.co.nz ([122.57.11.211] helo=[192.168.1.67]) by gavin.smtp.mailx.hosts.net.nz with esmtpsa authed as varteg.nz (TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_128_GCM:128) (Exim 4.96) (envelope-from ) id 1uk9QV-00EoM3-22 for internals@lists.php.net; Fri, 08 Aug 2025 10:49:35 +1200 Message-ID: <2d2cc2d2-5e62-41da-b697-cd752e48b1f2@varteg.nz> Date: Fri, 8 Aug 2025 10:49:27 +1200 Precedence: list list-help: list-post: List-Id: x-ms-reactions: disallow MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PHP-DEV] Protected inheritance hierarchies To: internals@lists.php.net References: <1754480697189.440122623.4156562731@yahoo.de> <387eee1d-c46f-460d-965e-c0500255ee57@app.fastmail.com> Content-Language: en-GB In-Reply-To: <387eee1d-c46f-460d-965e-c0500255ee57@app.fastmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Hosts-DKIM-Check: none From: weedpacket@varteg.nz (Morgan) On 2025-08-08 10:01, Rob Landers wrote: > > > For example, if |A::foo(): int| promises to always return a non-zero > integer, and a subclass overrides |foo()| to only return zero, that > violates the contract—even though the type signatures match perfectly. > This is the sort of semantic guarantee that LSP is about, and it is > discussed in nearly every reputable textbook and conference talk on OO > design. Languages like PHP can’t enforce these contracts, but the / > principle/ is what helps prevent subtle design bugs and behavioural > “surprises.” > Indeed. Those contracts only become enforceable in the type signature when your type system is robust enough to be able to express them. If you could declare |A::foo(): NonzeroInt| then the signature could prevent an overriding subclass C from returning zero from that function (while still allowing |C::foo(): PositiveInt|, say); the compiler can verify that the type of the expression C::foo() is written to return is of type NonzeroInt (or PositiveInt). In the absence of being able to declare such constraints in the type system, one has to fall back on other documentation about what behaviour is and isn't acceptable.