2013/3/19 Matīss Roberts Treinis <mrtrei...@gmail.com>:
> 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?

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 <mrtreinis at gmail dot com>

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to