Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:103604 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 99189 invoked from network); 13 Dec 2018 19:27:00 -0000 Received: from unknown (HELO mail-it1-f172.google.com) (209.85.166.172) by pb1.pair.com with SMTP; 13 Dec 2018 19:27:00 -0000 Received: by mail-it1-f172.google.com with SMTP id a6so4523606itl.4 for ; Thu, 13 Dec 2018 07:54:39 -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=J15XctcHD0YNDeOgIqXAslKZJjzkvyKtjmRst/ksrWc=; b=GQL3LnYIUwT2MMyytHFGVsExbIRCvFA4HC5J9HFwaUESM827ep+QBrtTsWDHRrT6GR vvQJLiDhFw81r5q01xhwqdBKJw+pxHK8iTOytUF+E7Bf+f3kRJa1Z7vTkdeY/IWlDSYZ 2Hl9FyG9QonBCazUPlJ+O9PQoeQUZ7rJeHFR9gQnZ5BT8QMULAxyTL1vI6tV3GWKZBHf GWbNJLwHG6a2pziWIOG51McDcTW1sWxo53DYUfS73kuJbGdm08bSGiGqXP2+4RU/ysFn +6Vty/QGYoP5gVCFJrUnsvgLulVkH65aNxbJQ0qyXVQ/KHhOCFlHN9AbCZjICNrw1ag8 0I1g== 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=J15XctcHD0YNDeOgIqXAslKZJjzkvyKtjmRst/ksrWc=; b=sc25+AVxmTbu0Snbs4QRrzQ0pbRUr3pbOZp3qlqdlCBUi3WDdOdVZaBRzDZsDGHDA5 c3fqQC4SnLtIiRC7WyRLsI+gM5DykHkNt/h3c6VdvWXsebMLATFipZpV73uH8DI1tD2d CQRHIyisotqqOR70lOQwU/lCiQpbbj9G3pE4JPB4iXb/zPjIyAohvQY+C3OQPVFQAQqt I+XvjFgU2WB90oBu2/OD28yRHf4MSznwkXULMNbf655oJZ5aIcEIbqD8rgUanvv+UAOE yTeM+BcfVTUqNkY2gJao7WLbtsA6BllJWUwISLBsQx5g3Q2l7eKLXpzNQfGeI31ncpKe EYBA== X-Gm-Message-State: AA+aEWan0Abl2l8/CZS5OlrZa8TAQx0E88iFZ7MKHps7A3U8APHiF7ZW o+hSCjwZJBMZ2NPowZr+EFZWIji5ihPZGHnZ7U4= X-Google-Smtp-Source: AFSGD/Ugm3k4D/rLGaBlFvnIWFU2QXSQtBYlwnYY4gU2p/DNjDhKLsmBmU+SLEQH+hAOIQmucZ4zvUh3mwXbzRFHTDY= X-Received: by 2002:a24:d441:: with SMTP id x62mr9978035itg.141.1544716479206; Thu, 13 Dec 2018 07:54:39 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Thu, 13 Dec 2018 16:54:26 +0100 Message-ID: To: benjamin.morel@gmail.com Cc: PHP Internals List Content-Type: multipart/alternative; boundary="00000000000005b5e6057ce95562" Subject: Re: [PHP-DEV] Typed properties - feature request From: ocramius@gmail.com (Marco Pivetta) --00000000000005b5e6057ce95562 Content-Type: text/plain; charset="UTF-8" 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 = $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 = true) > > > *Option 2: add a ReflectionProperty::unset() method:* > > function unset(string $property) > > This way, I could explicitly unset() every property that was not requested > 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 > --00000000000005b5e6057ce95562--