|
at 2010-08-18
in RFCs, 5.8-SERIES
by friebe
(0 comments)
Just shortly after announcing the removal of the original extension methods implementation we have now come up with a faster, less-clumsy and well-behaving extension method implementation - and have reactivated the corresponding RFC, scheduled for implementation in SVN next week. Declaration of an extension class is done as follows:
class ArrayListExtensions extends Object { static function __import($scope) { xp::extensions(__CLASS__, $scope); } /** * ArrayList::map() extension * * @param lang.types.ArrayList self * @return lang.types.ArrayList mapped */ public static function map(ArrayList $self, $block) { $mapped= ArrayList::newInstance($self->length); foreach ($self->values as $i => $value) { $mapped[$i]= $block($value); } return $mapped; } So the only thing necessary is registering extension methods from this class inside the __import() handler which is called on scope-import (via uses()). Note that only static methods whose first parameter is named self are valid extension methods. Of course the class may have any other number of non-extension methods.
To use extension methods simply add the extension class to the uses() statement of the scope you want to use them in:
uses('com.example.extensions.ArrayListExtensions'); class ArrayListDemo extends Object { /** * Main method * * @param string[] args */ public static function main($args) { $sorted= ArrayList::newInstance($args)->sorted(); Console::writeLine('sorted()= ', $sorted); } } This will be the output:
$ xp com.example.ArrayListDemo World Hello sorted()= lang.types.ArrayList[2]@{"Hello", "World"}
|
Subscribe
You can subscribe to the XP framework's news by using RSS syndication.
CategoriesNews General PHP5 Announcements RFCs Further reading Examples Editorial EASC Experiments Unittests Databases 5.8-SERIES Unicode Language 5.9-SERIES
RelatedFind related articles by a search for «Extension».
|