Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:98487 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 99720 invoked from network); 12 Mar 2017 05:58:47 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 12 Mar 2017 05:58:47 -0000 Authentication-Results: pb1.pair.com smtp.mail=theanomaly.is@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=theanomaly.is@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.128.174 as permitted sender) X-PHP-List-Original-Sender: theanomaly.is@gmail.com X-Host-Fingerprint: 209.85.128.174 mail-wr0-f174.google.com Received: from [209.85.128.174] ([209.85.128.174:32785] helo=mail-wr0-f174.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id BC/F4-38004-793E4C85 for ; Sun, 12 Mar 2017 00:58:47 -0500 Received: by mail-wr0-f174.google.com with SMTP id u48so86061125wrc.0 for ; Sat, 11 Mar 2017 21:58:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=AW8DeJZzrbluNfNjHtAmulZsopLeyOKGaa0HSmoKVOw=; b=APrkPm4PKcDKUU2K8FpUErBCX5te9df+em9mBWZqwUd13kqVM6vGEm86yMA0qo84/5 APFEAGAR2zyZlw1sL/3d3028KDRt8nii3AFzzHUT7lGFnpa3RzYx4bRIaxWC8RI//hLC MeTboT0AVnDIt3iJo1obr1iUQwEJqx877mp/AeusbibC8gOIFWAsF/EUq2EdO+I4Aopd FQyIoJJvgsQOtqUMoQgGd4iJikCKxbzBjEHs3+bWfmH5hfB+tV2DxLW797FbonOXd8OU 09MHZoSv+o1pt6NWHT04flxko3w0CWrf6Vz0l0lQA2VNkSDgUPRemf3+hknx1fhv0Rd3 YtLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=AW8DeJZzrbluNfNjHtAmulZsopLeyOKGaa0HSmoKVOw=; b=WJnMqhQpRTU4j+deFz7EjPliyGHASo59seLT1aKkY2duxrlNwBJVaHiCHp9KnEhqeL hpfnGVbN0JFUujMFltWGsWqSCBrWPWwrB6i3oUxtWh/W/9YRxP9vMNRPWPEC7HFPudb+ F/jA+iX1X03w5B9l0biD2yFen5HPJPB2/cGdd3K7i3JpEbkXS3a/YG5Aw1+zcxX4eCkF vG5Era0ev0MF6RyPz1P1dNmOfIS7Xxx4Rd6rM/YZIgJjzHG5C3KnjIYsmucHd24gIs5v X61uiQo/7KBgvLvy5FGxku1GGKjDTmcwDZEVyTpzc7lfEx4Bc2GPJjUPjb2Q2kVH9PZu LiTw== X-Gm-Message-State: AMke39mYn0Wmaf3SFJcWAqSN4heF4fLNClMnUZF6u5LJzLe0ynXsvG0eTtHGp53conr3Si/qHHaBCevatHU8AQ== X-Received: by 10.223.145.227 with SMTP id 90mr23501105wri.156.1489298324754; Sat, 11 Mar 2017 21:58:44 -0800 (PST) MIME-Version: 1.0 Received: by 10.80.143.35 with HTTP; Sat, 11 Mar 2017 21:58:44 -0800 (PST) In-Reply-To: References: Date: Sun, 12 Mar 2017 00:58:44 -0500 Message-ID: To: Michael Vostrikov Cc: PHP Internals Content-Type: multipart/alternative; boundary=94eb2c0d9f369c9a66054a824912 Subject: Re: [PHP-DEV] Type variants From: theanomaly.is@gmail.com (Sherif Ramadan) --94eb2c0d9f369c9a66054a824912 Content-Type: text/plain; charset=UTF-8 PHP already has horizontal inheritance. They're called traits. How is this an improvement in anyway? On Sat, Mar 11, 2017 at 1:36 AM, Michael Vostrikov < michael.vostrikov@gmail.com> wrote: > Hello. I have an idea which seems rather useful. I would like to know your > opinion, > > Let's say we have class Rectangle and need to have class Square which will > be used in some operations. We don't need any rectangle, this must be only > square. > What if we could describe a type which is the same as another type but have > some restrictions for properties? > > Something like this: > > class Rectangle > { > private $x; > private $y; > private $width; > private $height; > > public function __construct($x, $y, $width, $height) > { > $this->x = $x; > $this->y = $y; > $this->width = $width; > $this->height = $height; > } > } > > class Square variant of Rectangle > { > public function __match() > { > if ($this->width != $this->height) { > return ['width' => 'Width and height have to be the same']; > } > > return true; > } > } > > function drawSquare(Square $square) > { > // ... > } > > $rectangle = new Rectagle(0, 0, 10, 10); > drawSquare($rectangle); > > When drawSquare() is called, the check 'instanceof' is performed. It calls > magic method __match(). > If __match() does not return true, it throws an exception with the data > returned: > 'Order does not match OrderForCheckout. Reason: ...'. > > > Technically, we don't have method __match() in $rectangle object. So it can > be static, or a proxy object can be created here. > > The second variant can be extended. > We may not to inherit implementation of methods, and describe another > interface instead, for example new object can be read only. > > class Square variant of Rectangle > { > public function ___match() > { > // ... > } > > public function getWidth() > { > return parent::getWidth(); > } > } > > With some changes in syntax it can be defined in this way: > > class Square variant of Rectangle > { > ... > > getWidth: base::getWidth; > } > > > This is like inheritance, but semantically differs a little. > Inheritance has vertical direction, type variants have horisontal one. > Also type variants have an access for all methods and properties of base > class, even for private. > > > Another example. Let's say we have an order checkout process which is > splitted into 3 steps - cart for products, date page, delivery address > page. > This is not an input validation, this is a validation in business logic. > > class Order > { > public $products; > public $date; > public $address; > } > > class OrderForCheckout variant of Order > { > public function __match() > { > $errors = []; > if (empty($this->products)) { > $errors['products'] = 'Product list is empty'; > } > if ($this->date === null) { > $errors['date'] = 'Date is not selected'; > } > if (empty($this->products)) { > $errors['address'] = 'Address is not selected'; > } > > return (empty($errors) ? true : $errors); > } > } > > public function checkout(OrderForCheckout $order) > { > ... > } > > $order = findCurrentOrder(); > checkout($order); // if $order does not match requirements, the exception > will be thrown > --94eb2c0d9f369c9a66054a824912--