Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:66701 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 72841 invoked from network); 19 Mar 2013 12:36:08 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 19 Mar 2013 12:36:08 -0000 Authentication-Results: pb1.pair.com header.from=patrick.allaert@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=patrick.allaert@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.214.182 as permitted sender) X-PHP-List-Original-Sender: patrick.allaert@gmail.com X-Host-Fingerprint: 209.85.214.182 mail-ob0-f182.google.com Received: from [209.85.214.182] ([209.85.214.182:64129] helo=mail-ob0-f182.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id FC/A2-58539-7BB58415 for ; Tue, 19 Mar 2013 07:36:07 -0500 Received: by mail-ob0-f182.google.com with SMTP id va7so355280obc.13 for ; Tue, 19 Mar 2013 05:36:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=c0oWPsz1AfhJMqD0O2ZgwL7K7zUMizpVLYvQAuURCvQ=; b=Oz6NinnUWycxjoE5RA8QtCafzzi6BeRjE4qZwO9UKfJPWig7vu6oTDF0NYnFd5v3rL mQSLXNZ/A6FRxjk4R34QY5EcA/vAEGxx6/3NEZ553LZ3HPiS2hkEQPjjMjLNit303DMh D9djQ6CX/kYhoZtClDv6LUjtteoazGP/fbO188FEH2OwlRIbwfwjHQHTZ1dFxtlRSr7X 5KyEbABzW1UiN5I0OQKNVLhmXpRiTX79enL1P5M+0FY3Jc79QexAwRKncUvohlAPWd+b VMUJuazPh+r2cWuCEb17rlLxltVuUHHlJVyDTGTrB+TYVlkD0qQZNKpslbTwY6cMg1bp UZAA== MIME-Version: 1.0 X-Received: by 10.182.158.234 with SMTP id wx10mr630541obb.74.1363696564480; Tue, 19 Mar 2013 05:36:04 -0700 (PDT) Sender: patrick.allaert@gmail.com Received: by 10.76.163.38 with HTTP; Tue, 19 Mar 2013 05:36:04 -0700 (PDT) In-Reply-To: References: Date: Tue, 19 Mar 2013 13:36:04 +0100 X-Google-Sender-Auth: A34b1h3muXtHnpQIkZMck5Y636w Message-ID: To: =?UTF-8?Q?Mat=C4=ABss_Roberts_Treinis?= Cc: PHP Development Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Proposal: new magic methods for Object references. From: patrickallaert@php.net (Patrick ALLAERT) 2013/3/19 Mat=C4=ABss Roberts Treinis : > Proposal: > > Two additional magic methods for PHP objects - > > __refer(void): > > Refer method is called whenever object's identifier is assigned to > variable, if such method is present. > Example: > > $foo =3D new Bar; > $bar =3D $foo; //__refer invoked > > Return values: null to proceed by assigning the variable to objects > identifier. Any other value returned would be assigned to variable > which is to hold the reference in case of null. > > __unrefer($var): > > Unrefer method would be called whenever object's identifier is about > to be replaced by anything else and would hold that something else as > argument. > Example: > > $foo =3D new Bar; > $foo =3D false; //__unrefer invoked with false as argument > > Return values: null to proceed with default by replacing value, false > to not to replace value and keep the reference in variable. > > Real life use sample: > > class SomeObject{ > protected $count; > function __refer(){ > $this->count++; > } > } > > $foo =3D new Bar; > $bar =3D $foo; > $foobar =3D $bar; //SomeObject::$count =3D 2 > > ---------------------------------------------------- > > class SomeCustomString{ > protected $value; > function __unrefer($value){ > if(is_string($value)){ > $this->value =3D $value; > } > throw new UnexpectedValueException('This object can only hold str= ings'); > } > } > > $foo =3D new SomeCustomString; > $foo =3D 1; > > ------------------------------------------------------ > > Although I can't think of many uses to __refer I however can think of > one very useful use case for __unrefer - custom "types". I actually > did borrow the __unrefer functionality from SPL_Types PECL package > when trying to solve problem of how to validate contents of object > properties which are going to be set by other party and cant be > validated via set or get. I know there are ways to do this, but none > seemed pretty enough for me to actually implement. > > Any feedback on this is very welcome, especially from core developers > and potential users. What would be the issues, potential drawbacks, > other? For sure: *lot* of overhead on any object assignments: checking whether __refer() exist on the variable being assigned and whether __unrefer() exist on the destination variable. > Separate question to core developers would be, how hard it would be to > implement such functionality, or is this even possible with reasonable > effort? > > Thanks, regards > Matiss Roberts Treinis