prototypes for getters and setters.

From: Date: Wed, 13 Nov 2002 01:31:34 +0000
Subject: prototypes for getters and setters.
Groups: php.dev 
Request: Send a blank email to php-dev+get-90922@lists.php.net to get a copy of this message
Thanks to a little chat (and a few beers) with Zak at the conference, I got wondering if this syntax would be a sensible addition... The principle is to enable rapid prototyping of getter and setter methods..... class something { var getBanana setBanana $banana = 12; var getOrange $orange =1; function setOrange($value) {
       if ($value == $this->banana) {
           echo "mmh banana's and oranges are the same";         }
} } $t = new something; echo $t->getBanana(); echo $t->setBanana(3); echo $t->setOrange(3); syntax: var [getter method] [setter method] $variable .....; or any other ideas var (getBanana, setBanana) $banana function var getBanana, setBanana for $banana = '12'; // nice bit of token recycling..... var use getBanana and setBanana for $banana = '12'; from a quick play with zend_language_parser.y the additionall code to class_variable_decleration: would look a bit like this...
|     is_reference T_STRING T_VARIABLE  '=' static_scalar    {
               znode tmp;
               znode tmpthis;                            znode tmpfinalval;
               zend_copy_value(tmpthis, 'this', 5);
               tmpthis->type = IS_STRING;
               zend_do_declare_property(&$3, &$6 TSRMLS_CC);
               function.u.opline_num = CG(zend_lineno);
               zend_do_begin_function_declaration(&function, &$3, 1, $2.op_type TSRMLS_CC); }
                             # get the 'this' part..
               zend_do_fetch_globals(&tmpthis TSRMLS_CC);
               zend_do_begin_variable_parse(TSRMLS_C);
               fetch_simple_variable(&tmp, &tmpthis, 1 TSRMLS_CC);
               zend_do_fetch_property(&tmpfinalval, &tmp, &$6 TSRMLS_CC);}
                zend_do_return(&tmpfinalval, 1 TSRMLS_CC);
               zend_do_end_function_declaration(&$1 TSRMLS_CC);
                     }
Anyway before I get carried away and actually test this :) - anybody got any thoughts..... Regards Alan

« previous php.dev (#90922) next »