Re: template module
| From: | Alexander Merz | Date: | Tue, 07 Aug 2001 22:45:23 +0000 |
| Subject: | Re: template module | ||
| References: | 1 2 3 4 5 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-1318@lists.php.net to get a copy of this message | ||
> Ok, this is really a problem, find a solution and get rich and famous ;-)
Possible, I find a solution, now i can get rich and famous ;-)
I asked for a AUTOLOAD-mechanism like provided by Perl on the ZendEngine2-ML
( see the message ...). With this mechanism, it is possible to handle such
things in a clean manner.
Message:
---------------
> In Perl you can define a AUTOLOAD function, which will executed, when a non
> existing class function is called.
>
> Ex:
> sub AUTOLOAD {
> if ($AUTOLOAD =~ /.*::get(_\w+)/ ) {
> my $attr_name = $1;
> *{$AUTOLOAD} =
> sub {return $_[0]->{$attr_name}};
> return $_[0]->{$attr_name};
> }
> croak "No such method: $AUTOLOAD";
> }
>
> This is a nice mechanismen to create automatic accessor methods and upward
> compatibility. Could we introduced something in PHP?
>
> possible Ex:
> function _AUTOLOAD() {
> $functionname = $_AUTOLOAD['name'] ;
> $functionparam = $_AUTOLOAD['param'] ;
>
> if( preg_match("/get[.*]/", $functionname, $varname)) {
> $this -> $varname[1] = $functionparam[0] ;
> return true ; // function call handled by AUTOLOAD
> }
> elseif( preg_match("/set[.*]/", $functionname, $varname)) {
> $_AUTOLOAD['return'] = $this -> $varname[1] ; // assign return value
> return true ;
> } else {
> return false ; // function call can' be handled by AUTOLOAD
> }
> }
>
I agree that this would be useful, actually, I implemented this for
Zend Engine 1, so its really pretty simple to put it into PHP4.
What is the general consensus for this function with everyone else?
---------------