Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:98510 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 57493 invoked from network); 12 Mar 2017 18:01:56 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 12 Mar 2017 18:01:56 -0000 Authentication-Results: pb1.pair.com smtp.mail=php@fleshgrinder.com; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=php@fleshgrinder.com; sender-id=unknown Received-SPF: error (pb1.pair.com: domain fleshgrinder.com from 77.244.243.83 cause and error) X-PHP-List-Original-Sender: php@fleshgrinder.com X-Host-Fingerprint: 77.244.243.83 mx102.easyname.com Received: from [77.244.243.83] ([77.244.243.83:51970] helo=mx102.easyname.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id BD/4D-38004-21D85C85 for ; Sun, 12 Mar 2017 13:01:55 -0500 Received: from cable-81-173-135-7.netcologne.de ([81.173.135.7] helo=[192.168.178.20]) by mx.easyname.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.84_2) (envelope-from ) id 1cn7oU-00018n-8t; Sun, 12 Mar 2017 18:01:51 +0000 Reply-To: internals@lists.php.net References: <2a928258-af19-c15f-548f-eb348b3f5449@fleshgrinder.com> <8e4d7946-a7cf-7a02-a745-2451aab564d2@fleshgrinder.com> To: Michael Vostrikov , PHP Internals Message-ID: <8e4cfff5-7d0f-1dcc-1ee4-624e7ec1c523@fleshgrinder.com> Date: Sun, 12 Mar 2017 19:01:47 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-DNSBL-PBLSPAMHAUS: YES Subject: Re: [PHP-DEV] Type variants From: php@fleshgrinder.com (Fleshgrinder) On 3/12/2017 6:16 PM, Michael Vostrikov wrote: > No, thanks) I don't have real problem. I know some problems with usual > inheritance and try to suggest the tool to solve them. Problems which are > mostly kinds of Rectangle-Square problem, and where is some restrictions in > derived types. > As I said already, there is no problem if you just change the direction of the inheritance. class Square { private $x; private $y; private $w; public function __construct(int $x, int $y, int $w) { $this->x = $x; $this->y = $y; $this->w = $w; } public function draw(Canvas $canvas): void { } } class Rectangle extends Square { private $h; public function __construct(int $x, int $y, int $w, int $h) { parent::__construct($x, $y, $w); $this->height = $h; } public function draw(Canvas $canvas): void { } } If you want to ensure that you always get a Square if width and height match, no problem either. class Square { private $x; private $y; private $w; private $h; final protected function __construct( int $x, int $y, int $w, int $h ) { $this->x = $x; $this->y = $y; $this->w = $w; $this->h = $h; } public static function new(int $x, int $y, int $w): self { return new self($x, $y, $w, $w); } public function draw(Canvas $canvas): void { } } class Rectangle extends Square { public static function new( int $x, int $y, int $w, ?int $h = null ): parent { if ($h === null || $w === $h) { return new parent($x, $y, $w, $w); } return new self($x, $y, $w, $h); } } Added bonus here is the fact that we disabled the possibility for multiple constructor invocations, and are enforcing constructor argument invariants (which PHP does not). -- Richard "Fleshgrinder" Fussenegger