Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:115393 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 64012 invoked from network); 12 Jul 2021 01:15:19 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 12 Jul 2021 01:15:19 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id DDFFE1804AA for ; Sun, 11 Jul 2021 18:38:24 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2, SPF_HELO_NONE,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-qk1-f177.google.com (mail-qk1-f177.google.com [209.85.222.177]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sun, 11 Jul 2021 18:38:24 -0700 (PDT) Received: by mail-qk1-f177.google.com with SMTP id q190so16459715qkd.2 for ; Sun, 11 Jul 2021 18:38:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=newclarity-net.20150623.gappssmtp.com; s=20150623; h=from:message-id:mime-version:subject:date:in-reply-to:cc:to :references; bh=3WdX7IA03X7NxFpHDQUTok1ZDqgZWKSO6Wc9/Uc8dk4=; b=eAPsNCh7OLJ+A1231yrm9oNkQPyh1iQr4H9j5ejRANgXXZQblAC8SnWYUAYH7GB5cQ NCCKKxzWG6TNKItWLBeEq1MVJU82CxlWxVNysCDlPcmtCnWDU7Vdvh5VmOjD3QYE928U zlYoT9nTNZL+f7hjCHobCPrDXnACKEdTgP3VZoIbWhJKWb1XNTyK86H02g2CpwDTpscc 90WVT8WF11S/VWtjmTZaCveWYcs0R3RcFuFrEhRdt4THxF5rxq9B/BE6MdEjEDZ4bL31 Ni1lp3Rpyt76No+24lWLlKtjN6tlZr8UcKiYs/19ii2V8ZhRQKlZ2lHQZ4/tNU2M+8oh xPWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:message-id:mime-version:subject:date :in-reply-to:cc:to:references; bh=3WdX7IA03X7NxFpHDQUTok1ZDqgZWKSO6Wc9/Uc8dk4=; b=NTpcPk3+Y4t5f2fluZC1e/QMNMG9GYVX3KahrdU1eQOG6HvXwNkEj5ZShTJ3Xnd3Ux XFBQDRsbfpIJ5MrRl8iSVZGzCldS8tgploMMqjM/QC0569GlNyN5BWaYYQqoYCWxQ/HH XJ810V7JQj6XvE9sSL86eaT4NO7zzXfCQabAI2PHZZPM4c5Jv+gMS++cgIZoRpGC9KhT QynFsb0iJOPYV3qmgbJZSAdZ0BnBEh1FUzCyUzU48TgfEZTHq0jrK4sfkwt9UYfUGGP+ /410BajSmhn+gzakDUA4RH//DoJEevYYyYETmNv7KMeWKIaa8sLn0ClaJA1Z+sTwDV2R ghYg== X-Gm-Message-State: AOAM5303+uhqYKJfbf/5XvyvztalKRXbaKnXbriX7nNJk9xswxftOUAj zRj9LQA2vOGH5rnjhbQkLwG8Js96yZVu8qL7 X-Google-Smtp-Source: ABdhPJygPZqiqPrF+/NCFoBHos+duBPsX+gzN4tlTNETjY6cJniteaipgddPD9uR0xQPe2qJfGSdWw== X-Received: by 2002:a37:b881:: with SMTP id i123mr50471337qkf.207.1626053903472; Sun, 11 Jul 2021 18:38:23 -0700 (PDT) Received: from [192.168.1.10] (c-24-98-254-8.hsd1.ga.comcast.net. [24.98.254.8]) by smtp.gmail.com with ESMTPSA id c13sm4756900qtb.6.2021.07.11.18.38.22 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 11 Jul 2021 18:38:23 -0700 (PDT) Message-ID: <848DDFD2-979D-4CA6-8781-CCD3DEAF1349@newclarity.net> Content-Type: multipart/alternative; boundary="Apple-Mail=_D33CF852-8F73-4988-807B-0DC4FB77169C" Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.120.23.2.7\)) Date: Sun, 11 Jul 2021 21:38:22 -0400 In-Reply-To: <0d28ee72-accd-4dfc-a8e0-018c99e4a86d@www.fastmail.com> Cc: php internals To: Larry Garfield References: <0d28ee72-accd-4dfc-a8e0-018c99e4a86d@www.fastmail.com> X-Mailer: Apple Mail (2.3608.120.23.2.7) Subject: Re: [PHP-DEV] Type casting syntax From: mike@newclarity.net (Mike Schinkel) --Apple-Mail=_D33CF852-8F73-4988-807B-0DC4FB77169C Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > On Jul 11, 2021, at 7:19 PM, Larry Garfield = wrote: >=20 > What are the use cases for integrated object to object (ie, class to = class) conversion in PHP? =20 > I'm not entirely clear on what the use case is in PHP. When would = that be superior to "just write an asFoo() method and move on with = life"? Reading your question triggered the following thoughts. They are not = use-case's per-se because I can't remember them, but I know I have felt = the need for these each at least once, and especially the last one. =20 Note that you cannot currently do any of these with an asFoo() method, = at least not directly. 1. It would be nice to be able to create a new instance of a parent, = grandparent class, etc. class given the properties of a child class = instance. Maybe: class Progenitor { public $surname; function __construct($surname) { $this-> surname =3D $surname; } } class Offspring extends Progenitor {} $offspring =3D new Offspring('Schinkel'); $progenitor =3D clone $offspring asinstanceof Progenitor echo get_class($progenitor); // prints: Progenitor echo $progenitor->surname; // prints: Schinkel 2. Similarly it would be nice to be able to treat an object as an = instance of its parent/grandparent/etc. where (parentof $instance) =3D=3D=3D= $instance $progenitor =3D $offspring asinstanceof Progenitor echo get_class($progenitor); // prints: = Progenitor echo $progenitor =3D=3D=3D $offspring ? 'frue' : 'false; // prints: = true Note I have no idea if having two instances where references are equal = but they are different classes would have unintended consequences, so = this might not be a good idea, not sure. But it would be nice to be = able to treat an object as its parent from time to time, if it is = possible. 3. It would also be nice to create a child class starting with an = instance of a parent as its base. See example for #5. It should work the same. 4. Similarly it might be nice to be able to (somehow) assign a child = class' identity and properties to an instance of its = parent/grandparent/etc. where (childof $instance) =3D=3D=3D $instance = although I have absolutely no idea how it this would work syntactically. ??? 5. And probably the functionality I've wanted most in this area =E2=80=94 = which is admittedly only tangentially-related =E2=80=94 is to be able to = assign to $this so we could do something like clone an object to get an = equivalent object. Or if case #1 or #2 above was possible, by cloning an = instance of its parent/grandparent/etc. would initialize for that = object. For example: class Foo { public $value; function __construct(int $value) { $this->value =3D $value; } function fromFoo(Foo $foo) { $this =3D clone $foo; // <-- assigning $this replaces all = state to be equal to the cloned instance } } $f1 =3D new Foo(100); $f2 =3D new Foo(0); $f2->fromFoo($f1); echo $f2->value; // prints 100 > Scalar conversion I can see, but every time someone suggests adding = siblings to __toString(), there's major pushback. Look ma, no magic methods! :-) -Mike= --Apple-Mail=_D33CF852-8F73-4988-807B-0DC4FB77169C--