Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:67147 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 88580 invoked from network); 25 Apr 2013 12:47:26 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 25 Apr 2013 12:47:26 -0000 Authentication-Results: pb1.pair.com smtp.mail=rasmus@mindplay.dk; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=rasmus@mindplay.dk; sender-id=unknown Received-SPF: error (pb1.pair.com: domain mindplay.dk from 209.85.212.44 cause and error) X-PHP-List-Original-Sender: rasmus@mindplay.dk X-Host-Fingerprint: 209.85.212.44 mail-vb0-f44.google.com Received: from [209.85.212.44] ([209.85.212.44:52700] helo=mail-vb0-f44.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 1D/26-50361-DD529715 for ; Thu, 25 Apr 2013 08:47:25 -0400 Received: by mail-vb0-f44.google.com with SMTP id e13so235116vbg.3 for ; Thu, 25 Apr 2013 05:47:21 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:x-received:date:message-id:subject:from:to :content-type:x-gm-message-state; bh=Lt1gAbA07tlZzgyamlcUSOq4KTg1qkHTXzP195URHCw=; b=SxZsavhtUGHWWUi04T+tVSJ846CF1fSLICHonPpKD+Uy/ofpUj+D8FPnbPKrhi4skT Rvo11HqioVVBAo8IAcYZqc9Ebki6TzYEwMZmTcoDU5/RSywC6SEcSY5CornuxkIoudO7 /tsQYtil/jTEyOhBPsU4fKIimsimYm92I8HaxW/SpNPb6Kw8OnZbNCbgcSCH+NiiEYCL qZTo1Jwp++zxVuIexFg0Zy3un1aVfeG45HOQzXz2mDyG3ij5sJE41nqNy7B8U4rMbRce s+wSedDVj7XeU2TevdVXTLNFgPz0sG9s8Dk2zI4rluj4eNMsCiuaukI2hzNM3odagyaQ r3xw== MIME-Version: 1.0 X-Received: by 10.52.18.173 with SMTP id x13mr13869664vdd.121.1366894041645; Thu, 25 Apr 2013 05:47:21 -0700 (PDT) Received: by 10.58.28.134 with HTTP; Thu, 25 Apr 2013 05:47:21 -0700 (PDT) Date: Thu, 25 Apr 2013 08:47:21 -0400 Message-ID: To: PHP internals Content-Type: multipart/alternative; boundary=bcaec50405c0cc861704db2ed1d4 X-Gm-Message-State: ALoCoQk7jHOoRWvZI1kntZn/LG00MNcPu4fvMuv9Z9NYuoxa09eNsI6028mvP4mT09qHyhMvq4tT Subject: property de-referencing From: rasmus@mindplay.dk (Rasmus Schultz) --bcaec50405c0cc861704db2ed1d4 Content-Type: text/plain; charset=ISO-8859-1 Okay, No one seemed extremely interested in my notes about static type-references- I want to bring up something closely related that could possibly be even more useful, in particular for things like form-helpers, which remains as one of the things that just can't be done in PHP as elegantly as it can in some other languages. I'm not sure if de-referencing is the correct term? I'm talking about the ability to use a property-reference indirectly - meaning, don't read the property, don't write the property, but reference the property in a way that permits you to read/write the property later on. In this example, I'm using ^ as the de-reference operator: class User { public $email; } $user = new User(); $form = new FormHelper($user); echo $form->textInput(^$user->email); Note the ^ operator in the last line - we're not passing the value of $user->email to FormHelper::textInput() but a reference to it... assuming a new internal PropertyReference class, it's declaration might look like this: class FormHelper { public function textInput(PropertyReference $prop) { $name = $prop->className . '[' . $prop->propertyName . ']'; $value = $prop->getValue(); return ""; } .... } The new PropertyReference class might have an interface like the following: class PropertyReference { public readonly object $object; // the object that was referenced public readonly string $className; // the type of object that was referenced public readonly string $propertyName; // the name of the property that was referenced public function getValue(); // read and return the property-value public function setValue($value); // write the property-value } For a statement like this: $prop = ^$user->email; The ^ operator does something equivalent to: $prop = new PropertyReference(); $prop->object = $user; $prop->className = get_class($user); $prop->propertyName = 'email'; $prop->getValue = function () use ($user) { return $user->email; }; $prop->setValue = function ($value) use ($user) { $user->email = $value; }; So essentially, you get all the information about the object and property, and a way to read/write that property, with one character - something that would enable hugely convenient APIs for things like form-helpers, data-mappers and object/relational-mappers. When the object-type is known in IDEs (as it is in properly documented code) this also addresses the same issue as static type-references, allowing for static analysis and automated refactoring, but with a much shorter and more convenient syntax - not needing to explicitly reference the class-name, and with the added benefit of knowing the instance, both of which are hugely important for form-helpers and mappers. Note that the getValue() and setValue() closures resolve properties the usual way - that is, you can de-reference magic properties too, since no attempt is made to read or write the property until getValue() or setValue() is made. Most use-cases for referencing class and property-names are situations like these, so this kind of kills two birds with one stone - a static way to reference properties, but with the addition of providing the actual object-context. And the shortest possible syntax. What do you think? - Rasmus Schultz --bcaec50405c0cc861704db2ed1d4--