Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:103608 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 44780 invoked from network); 14 Dec 2018 15:51:01 -0000 Received: from unknown (HELO mail-io1-f49.google.com) (209.85.166.49) by pb1.pair.com with SMTP; 14 Dec 2018 15:51:01 -0000 Received: by mail-io1-f49.google.com with SMTP id m19so4294132ioh.3 for ; Fri, 14 Dec 2018 04:18:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=CmINQzHpKka7KlwxsTvV0HrmKU/D9qZFzzMSjulQd9Y=; b=R/qfrVim31ZY5xPtmXd0KtQSLENCqihytGZwXo93TwsD+Rf7RDQz7PvXxWUq6Q6Qs1 maWOW/xRgPJ2J2Eck7vSxeONDPG0pWKA8ifFhhJC+it0flFcYaTv+RO+giiKv+Qcn+Wo lwq+hk0nj4JyQJ4kyd6dCTDax+wFs/LmKyDVvkLhKeVyfDtbSVa1YJE0bgOZiBAtsvKX R6eQhB7nnyUJc2Afc4+CeVsPtFYZ2bWGKeqL1S1dC3qZERS0asQjAFCfRPvbZT3bR8TW oSOeOFv8DcxTFppvsruulz8q/5T3AlmgOCXh3Jc1CtSlMB0MADLeeS3Wd2jCJWSpEpco iK4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=CmINQzHpKka7KlwxsTvV0HrmKU/D9qZFzzMSjulQd9Y=; b=rD3ry/sTy049Z0lCPHpvWXGQCuA2tOIOiZ0mBCkA8m75KS94MJDHeVfwmBYWPzCitX 7ntqvmIP16BlP32dKMQpDzuWL/AnnirgPhYyHYsoOZTkc6/WnH83zpYyv2kASAVr9T4L msTZHow15IVTwPF0sfN0hmt4saUTnZBzl3vaLILeN3mm/nZ3ARGrq1XmSoMoS3hMA+9r HkmjyoJVRskXopxMVE5wQnb0d49oVMlhqsIw+ONxmlzts9lEwcKCCxeOsm3ZZvSXi7Yo 6QkNyWzYeDUuVb6cITnXGV1bQa+INiggDAVns1XywNvz4Bq7DKil04hr0pGTV0r5+RoH WfzA== X-Gm-Message-State: AA+aEWY23A9C5KNvk188K9GYNunhWmB79e4//4UPDeWFWt7woixAya8Z PfkREWPscWpgEqYSHJFH87yVUImEZHkvCAD9wkw= X-Google-Smtp-Source: AFSGD/WXHWGQdw9nTGh9BvfvHcpBAemW6Fv4Vxq5DZyyjH/v5q7oDbhcIL7Hu9V5XSIqWL3BULMER4BaGPsw3uBUALM= X-Received: by 2002:a6b:f017:: with SMTP id w23mr2141703ioc.12.1544789933567; Fri, 14 Dec 2018 04:18:53 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Fri, 14 Dec 2018 13:18:40 +0100 Message-ID: To: Marco Pivetta Cc: PHP Internals Content-Type: multipart/alternative; boundary="0000000000003e3394057cfa6f74" Subject: Re: [PHP-DEV] Typed properties - feature request From: benjamin.morel@gmail.com (Benjamin Morel) --0000000000003e3394057cfa6f74 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Benjamin said: (...) Regarding your suggestion, unfortunately not, (array) also returns > default values for properties: Marco said: Yes, but that's where you should unset all properties upfront: > class A { > protected $a =3D 1; > } > $r =3D new ReflectionClass('A'); > $a =3D $r->newInstanceWithoutConstructor(); > (function (A $a) { unset($a->a); })->bindTo(null, A::class)($a); > print_r((array) $a); > Array > ( > ) Ah, genius! I did not think we could already use Closures for this. Scratch that then, there is no need for an extra feature =F0=9F=91=8D Thanks Marco. On Thu, 13 Dec 2018 at 16:54, Marco Pivetta wrote: > Hi Ben, > > Such logic is already possible by casting the object to `(array)`, and > therefore receiving a list of all properties that are explicitly set. > > Also, this seems like something I've already started from Doctrine 3 (but > couldn't pursue further due to time constraints). > > Marco Pivetta > > http://twitter.com/Ocramius > > http://ocramius.github.com/ > > > On Thu, Dec 13, 2018 at 3:04 PM Benjamin Morel > wrote: > >> Hi internals, >> >> I have a feature request related to upcoming typed properties, that I'd >> like to discuss here first. >> I'm building a data mapper that will target PHP 7.4, and will make heavy >> use of typed properties. >> >> One of the features that I want to have available, is allowing to load a >> partial object, by giving a list of properties to load: >> >> $user =3D $userRepo->load(123, ['status', 'followerCount']); >> >> Under the hood, load() will create a new User object using >> ReflectionClass:newInstanceWithoutConstructor(), get the data from the >> database and populate the requested fields using >> ReflectionProperty::setValue(). Now when I do: >> >> $userRepo->save($user); >> >> The repo will use the new ReflectionProperty::isInitialized() method to >> locate which fields are initialized, and only save these to the database= . >> The problem is, newInstanceWithoutConstructor() *initializes properties = to >> their default value*, so save() has no way to know whether initialized >> properties have been explicitly set during load(), or are just default >> value (note: repos are stateless). >> >> To overcome this problem, I can think of 2 possible options: >> >> *Option 1: add an optional parameter to >> ReflectionClass::newInstanceWithoutConstructor(), to not initialize >> properties to their default value:* >> >> function newInstanceWithoutConstructor(bool $initializeProperties =3D tr= ue) >> >> >> *Option 2: add a ReflectionProperty::unset() method:* >> >> function unset(string $property) >> >> This way, I could explicitly unset() every property that was not request= ed >> during load(). >> Currently, there is no way to unset() a protected property from outside >> the >> object (i.e. in the data mapper). >> >> >> Note that Option 1 would have a slight performance advantage in my case, >> while Option 2 may have more use cases outside of mine. >> >> What do you think? >> Cheers, >> Ben >> > --0000000000003e3394057cfa6f74--