Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:66700 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 70304 invoked from network); 19 Mar 2013 12:13:32 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 19 Mar 2013 12:13:32 -0000 Authentication-Results: pb1.pair.com smtp.mail=mrtreinis@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=mrtreinis@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.215.49 as permitted sender) X-PHP-List-Original-Sender: mrtreinis@gmail.com X-Host-Fingerprint: 209.85.215.49 mail-la0-f49.google.com Received: from [209.85.215.49] ([209.85.215.49:62094] helo=mail-la0-f49.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id C9/32-58539-B6658415 for ; Tue, 19 Mar 2013 07:13:32 -0500 Received: by mail-la0-f49.google.com with SMTP id fs13so737771lab.36 for ; Tue, 19 Mar 2013 05:13:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:date:message-id:subject:from:to :content-type; bh=Up/tvFSmgpqZSVO1IlbB2ATWKM+9U8oFllfFcjtQzcY=; b=m6Wbp+PLIzCocuffft5q6DNmBIHldshrSGSD+qBs9RnKq0kGYGrn7Wnv/udQg8vVeX HhIjXehJYA5IJINtCD0hV9ztb01IEr2yhMcJqMGlad5W9DLkfDTV6rqAM+ItXLtBSzy0 CVXdOF3KxJ8wsOH1u9tYDNLisNdMsYxhe9v5rMXvmEPNLkailv4EcZsoV5FYWPLO/BkH J66f4Dx3nueVwgERgphJACRt8Xc8wGyb6AP1c1lgSePL6/NdYMjH54iI0bxz0eItu6ah XOIwWb2TLLqBDGcK2C2q2l2vRa0FiSHxS1B4ZaWyFTl7nfmI0iQDGRtWMsQFxHlctPc8 DaTw== MIME-Version: 1.0 X-Received: by 10.112.100.103 with SMTP id ex7mr7856207lbb.80.1363695208520; Tue, 19 Mar 2013 05:13:28 -0700 (PDT) Received: by 10.114.14.131 with HTTP; Tue, 19 Mar 2013 05:13:28 -0700 (PDT) Date: Tue, 19 Mar 2013 14:13:28 +0200 Message-ID: To: PHP Development Content-Type: text/plain; charset=UTF-8 Subject: Proposal: new magic methods for Object references. From: mrtreinis@gmail.com (=?UTF-8?Q?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 = new Bar; $bar = $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 = new Bar; $foo = 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 = new Bar; $bar = $foo; $foobar = $bar; //SomeObject::$count = 2 ---------------------------------------------------- class SomeCustomString{ protected $value; function __unrefer($value){ if(is_string($value)){ $this->value = $value; } throw new UnexpectedValueException('This object can only hold strings'); } } $foo = new SomeCustomString; $foo = 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? 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