rfc:anonymous_classes
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
rfc:anonymous_classes [2015/03/04 19:08] – philstu | rfc:anonymous_classes [2015/04/27 14:33] – implemented nikic | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== PHP RFC: Anonymous Classes ====== | ====== PHP RFC: Anonymous Classes ====== | ||
- | * Version: 0.5 | + | * Version: 0.6 |
* Date: 2013-09-22 | * Date: 2013-09-22 | ||
* Author: Joe Watkins < | * Author: Joe Watkins < | ||
- | * Status: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
Line 27: | Line 27: | ||
}); | }); | ||
</ | </ | ||
- | |||
- | ===== Use Cases ===== | ||
- | |||
- | Following the general advice, the area of code testing appears to present the most significant number of use cases, however, where anonymous classes are a part of a language they do find their way into many areas of development, | ||
- | |||
- | Here is one example, which covers converting PSR-7 middleware to Laravel 5-style middleware. | ||
- | |||
- | <code php> | ||
- | <?php | ||
- | $conduit-> | ||
- | public function __invoke($request, | ||
- | { | ||
- | $laravelRequest = mungePsr7ToLaravelRequest($request); | ||
- | $laravelNext | ||
- | return $next(mungeLaravelToPsr7Request($request), | ||
- | }; | ||
- | $laravelMiddleware = new SomeLaravelMiddleware(); | ||
- | $response = $laravelMiddleware-> | ||
- | return mungeLaravelToPsr2Response($response); | ||
- | } | ||
- | }); | ||
- | </ | ||
- | |||
- | Anonymous classes do present the opportunity to create the first kind of nested class in PHP, you might nest for slightly different reasons to creating an anonymous class, so that deserves some discussion; | ||
- | |||
- | <code php> | ||
- | <?php | ||
- | class Outside { | ||
- | protected $data; | ||
- | | ||
- | public function __construct($data) { | ||
- | $this-> | ||
- | } | ||
- | | ||
- | public function getArrayAccess() { | ||
- | return new class($this-> | ||
- | public function offsetGet($offset) { return $this-> | ||
- | public function offsetSet($offset, | ||
- | public function offsetUnset($offset) { unset($this-> | ||
- | public function offsetExists($offset) { return isset($this-> | ||
- | }; | ||
- | } | ||
- | } | ||
- | </ | ||
- | |||
- | Note: Outer is extended not for access to $this-> | ||
- | |||
- | In the simple example above Outer:: | ||
- | |||
- | By making getArrayAccess private the anonymous class it creates can be said to be a private class. | ||
- | |||
- | This increases the possibilities for grouping of your objects functionality, | ||
- | |||
- | The alternative to the above is the following: | ||
- | |||
- | <code php> | ||
- | class Outer implements ArrayAccess { | ||
- | public $data; | ||
- | | ||
- | public function __construct($data) { | ||
- | $this-> | ||
- | } | ||
- | | ||
- | public function offsetGet($offset) { return $this-> | ||
- | public function offsetSet($offset, | ||
- | public function offsetUnset($offset) { unset($this-> | ||
- | public function offsetExists($offset) { return isset($this-> | ||
- | | ||
- | } | ||
- | </ | ||
- | |||
- | Pass-by-reference is not used in the examples above, so behaviour with regard to $this-> | ||
- | |||
- | How you choose to do it for any specific application, | ||
- | |||
- | Various use cases have been suggested on the mailing list: http:// | ||
- | |||
- | ===== Use Cases from the Community ===== | ||
- | Below are some excerpts of the discussion on internals mailing list including some use cases that the community see: | ||
- | |||
- | ---- | ||
- | The use case is one-time usage of an " | ||
- | probably pass callbacks into a " | ||
- | <code php> | ||
- | $x = new Callback(function() { | ||
- | /* do something */ | ||
- | }); | ||
- | |||
- | /* vs */ | ||
- | |||
- | $x = new class extends Callback { | ||
- | public function doSometing() | ||
- | { | ||
- | /* do something */ | ||
- | } | ||
- | }; | ||
- | </ | ||
- | |||
- | Imagine you have several abstract methods in one interface/ | ||
- | would need several callbacks passed to the constructor. | ||
- | Also ' | ||
- | |||
- | ---- | ||
- | It also avoids usage of these classes outside the scope where they are | ||
- | defined... | ||
- | |||
- | ---- | ||
- | It is a widely used pattern in object oriented programming ... where you code against interfaces : | ||
- | <code php> | ||
- | $subject-> | ||
- | function update(SplSubject $s) { | ||
- | printf(" | ||
- | } | ||
- | }); | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | It would also definitely be very useful for mocking in tests. We could create | ||
- | on-the-fly implementations for interfaces, avoiding using complex | ||
- | mocking API (PHPUnit or others). | ||
- | |||
- | ---- | ||
- | There are many use cases where anonymous classes are useful, even in the | ||
- | presence of lambdas. I use them quite often when dealing with graphical | ||
- | interfaces and templates. Here is an example: | ||
- | |||
- | <code php> | ||
- | abstract class MyFancyHtmlListView extends UI { | ||
- | protected function IsHeaderVisible(){ return true; } | ||
- | protected function GetListItemMenu(){ return null; } | ||
- | protected function OnItemClick( $item ){ } | ||
- | protected abstract function RenderListItem( $item ); | ||
- | public function Render(){ | ||
- | // echo ... | ||
- | } | ||
- | } | ||
- | </ | ||
- | With anonymous classes we could do something like this: | ||
- | |||
- | <code php> | ||
- | <?= new class extends HTMLView { | ||
- | protected function IsHeaderVisible(){ | ||
- | return false; | ||
- | } | ||
- | protected function RenderListItem( $item ){ | ||
- | // echo ... | ||
- | } | ||
- | } | ||
- | </ | ||
- | The biggest advantage is that a missing RenderListItem could be statically | ||
- | verified. | ||
- | |||
- | It is just a pattern that follows a different way of thinking: Instead of | ||
- | having a list of parameters (including lambdas), we have standard methods | ||
- | that take advantage of all the nice properties of OOP such as abstraction, | ||
- | inheritance and polymorphism. | ||
- | |||
- | ---- | ||
- | |||
- | Playing devil' | ||
- | expressive? | ||
- | |||
- | Take for example an API where you'd typically wrap a method call in | ||
- | try/catch blocks to handle the various " | ||
- | maybe have a UserDisabled exception, a UserAlreadyLoggedIn exception, a | ||
- | UserPasswordIncorrect exception, etc. | ||
- | |||
- | With the addition of this syntactic sugar, the method could instead accept | ||
- | an anonymous class with a onDisabled, onLoggedIn, onPasswordIncorrect | ||
- | methods. | ||
- | |||
- | Perhaps it would also have a performance benefit over cascading through | ||
- | catch blocks? Though someone else would have to confirm that. | ||
- | |||
- | ---- | ||
- | |||
- | Overriding a specific method in a class is one handy use. Instead of creating | ||
- | a new class that extends the original class, you can just use an anonymous | ||
- | class and override the methods that you want. | ||
- | |||
- | E.G; You can to the following: | ||
- | |||
- | <code php> | ||
- | use Symfony\Component\Process\Process; | ||
- | |||
- | $process = new class extends Process { | ||
- | public function start() { | ||
- | /* ... */ | ||
- | } | ||
- | }; | ||
- | </ | ||
- | instead of the following: | ||
- | <code php> | ||
- | namespace My\Namespace\Process; | ||
- | |||
- | use Symfony\Component\Process\Process as Base; | ||
- | |||
- | class Process extends Base { | ||
- | public function start() { | ||
- | /* ... */ | ||
- | } | ||
- | } | ||
- | |||
- | $process = new \My\Namespace\Process\Process; | ||
- | </ | ||
- | |||
- | ===== Backward Incompatible Changes ===== | ||
- | |||
- | New syntax that will fail to parse in previous versions, so no BC breaks. | ||
- | |||
- | ===== Proposed PHP Version(s) ===== | ||
- | |||
- | 7.0 | ||
- | |||
- | ===== SAPIs Impacted ===== | ||
- | |||
- | All | ||
- | |||
- | ===== Impact to Existing Extensions ===== | ||
- | |||
- | No impact on existing libraries | ||
- | |||
- | ===== Open Issues ===== | ||
- | |||
- | The question of whether or not to disable serialization for anonymous objects. | ||
- | |||
- | ===== Future Scope ===== | ||
- | |||
- | The changes made by this patch mean named nested classes are easier to implement (by a tiny bit). | ||
- | |||
- | ===== Proposed Voting Choices ===== | ||
- | |||
- | Straight forward, we should have this, we should not have this. | ||
===== Syntax and Examples ===== | ===== Syntax and Examples ===== | ||
Line 330: | Line 96: | ||
===== Inheritance/ | ===== Inheritance/ | ||
- | |||
- | Extending classes works just as you'd expect. | ||
- | |||
- | <code php> | ||
- | <?php | ||
- | |||
- | class Foo {} | ||
- | |||
- | $child = new class extends Foo {}; | ||
- | |||
- | var_dump($child instanceof Foo); | ||
- | </ | ||
Extending classes works just as you'd expect. | Extending classes works just as you'd expect. | ||
Line 373: | Line 127: | ||
</ | </ | ||
- | ===== Comparisons | + | ===== Reflection |
- | Multiple anonymous classes created in the same position | + | The only change to reflection is to add ReflectionClass:: |
+ | |||
+ | ===== Serialization ===== | ||
+ | |||
+ | Serialization is not supported, and will error just as anonymous functions do. | ||
+ | |||
+ | ===== Internal Class Naming ===== | ||
+ | |||
+ | The internal | ||
<code php> | <code php> | ||
- | <?php | + | function my_factory_function(){ |
+ | return new class{}; | ||
+ | } | ||
+ | </code> | ||
+ | get_class(my_factory_function()) would return " | ||
+ | |||
+ | <code php> | ||
+ | class mine {} | ||
+ | |||
+ | new class extends mine {}; | ||
+ | </ | ||
+ | |||
+ | This class name will be " | ||
+ | |||
+ | Multiple anonymous classes created in the same position (say, a loop) can be compared with `==`, but those created elsewhere will not match as they will have a different name. | ||
+ | |||
+ | <code php> | ||
$identicalAnonClasses = []; | $identicalAnonClasses = []; | ||
Line 405: | Line 183: | ||
Both classes where identical in every way, other than their generated name. | Both classes where identical in every way, other than their generated name. | ||
- | ===== Reflection | + | ===== Use Cases ===== |
- | The only change to reflection | + | Code testing presents the most significant number of use cases, however, where anonymous classes are a part of a language they do find their way into many use cases, not just testing. Whether it is technically correct |
- | ===== Code Paths ===== | + | A few quick points: |
- | Code such as: | + | * Mocking tests becomes easy as pie. Create on-the-fly implementations for interfaces, avoiding using complex mocking APIs. |
+ | * Keep usage of these classes outside the scope they are defined in | ||
+ | * Avoid hitting the autoloader for trivial implementations | ||
+ | |||
+ | Tweaking existing classes which only change a single thing can make this very easy. Taking an example from the [[https:// | ||
<code php> | <code php> | ||
- | while ($i++<10) { | + | // PHP 5.x |
- | | + | class MyLogger { |
- | /* ... */ | + | public function log($msg) { |
- | } | + | |
+ | } | ||
} | } | ||
+ | |||
+ | $pusher-> | ||
+ | |||
+ | // New Hotness | ||
+ | $pusher-> | ||
+ | public function log($msg) { | ||
+ | print_r($msg . " | ||
+ | } | ||
+ | }); | ||
</ | </ | ||
- | will fail to execute, however code such as: | + | This saved us making a new file, or placing the class definition at the top of the file or somewhere a long way from its usage. For big complex actions, or anything that needs to be reused, that would of course be better off as a named class, but in this case it's nice and handy to not bother. |
+ | |||
+ | If you need to implement a very light interface to create a simple dependency: | ||
<code php> | <code php> | ||
- | while ($i++<10) { | + | $subject-> |
- | | + | function update(SplSubject |
- | } | + | printf(" |
+ | } | ||
+ | }); | ||
</ | </ | ||
- | will work as expected: the definition will be re-used, creating a new object. | + | Here is one example, which covers converting PSR-7 middleware to Laravel 5-style middleware. |
- | ===== Internal Class Naming ===== | + | <code php> |
+ | <?php | ||
+ | $conduit-> | ||
+ | public function __invoke($request, | ||
+ | { | ||
+ | $laravelRequest | ||
+ | $laravelNext | ||
+ | return $next(mungeLaravelToPsr7Request($request), | ||
+ | }; | ||
+ | $laravelMiddleware | ||
+ | $response | ||
+ | return mungeLaravelToPsr2Response($response); | ||
+ | } | ||
+ | }); | ||
+ | </ | ||
- | The internal name of an anonymous class is generated based on the scope which created it, such that: | + | Anonymous classes do present the opportunity to create the first kind of nested class in PHP. You might nest for slightly different reasons to creating |
<code php> | <code php> | ||
- | function | + | <?php |
- | return new class{}; | + | class Outside { |
+ | protected $data; | ||
+ | |||
+ | public | ||
+ | $this-> | ||
+ | | ||
+ | |||
+ | public function getArrayAccess() { | ||
+ | | ||
+ | public function offsetGet($offset) { return $this-> | ||
+ | public function offsetSet($offset, | ||
+ | public function offsetUnset($offset) { unset($this-> | ||
+ | public function offsetExists($offset) { return isset($this-> | ||
+ | }; | ||
+ | } | ||
} | } | ||
</ | </ | ||
- | get_class(my_factory_function()) would return '' | + | Note: Outer is extended not for access to $this-> |
- | And in the following | + | In the simple |
+ | |||
+ | By making getArrayAccess private the anonymous class it creates can be said to be a private class. | ||
+ | |||
+ | This increases the possibilities for grouping of your objects functionality, | ||
+ | |||
+ | The alternative to the above is the following: | ||
<code php> | <code php> | ||
- | class Other { | + | class Outer implements ArrayAccess |
- | public function | + | public $data; |
- | | + | |
+ | public function | ||
+ | | ||
} | } | ||
+ | | ||
+ | public function offsetGet($offset) { return $this-> | ||
+ | public function offsetSet($offset, | ||
+ | public function offsetUnset($offset) { unset($this-> | ||
+ | public function offsetExists($offset) { return isset($this-> | ||
+ | | ||
} | } | ||
</ | </ | ||
- | get_class((new Other())-> | + | Pass-by-reference is not used in the examples above, so behaviour with regard to $this-> |
- | Finally: | + | How you choose to do it for any specific application, |
+ | Various use cases have been suggested on the mailing list: http:// | ||
+ | |||
+ | ---- | ||
+ | |||
+ | The use case is one-time usage of an " | ||
+ | probably pass callbacks into a " | ||
<code php> | <code php> | ||
- | var_dump(get_class(new class{})); | + | $x = new Callback(function() { |
+ | /* do something */ | ||
+ | }); | ||
+ | |||
+ | /* vs */ | ||
+ | |||
+ | $x = new class extends Callback | ||
+ | public function doSometing() | ||
+ | { | ||
+ | /* do something */ | ||
+ | } | ||
+ | }; | ||
</ | </ | ||
- | in the global scope would return | + | Imagine you have several abstract methods |
+ | would need several callbacks passed to the constructor. | ||
+ | Also '$this' | ||
- | ===== Implementation ===== | + | Overriding a specific method in a class is one handy use. Instead of creating |
+ | a new class that extends the original class, you can just use an anonymous | ||
+ | class and override the methods that you want. | ||
- | https://github.com/php/php-src/pull/1118 | + | E.G; You can to the following: |
+ | |||
+ | <code php> | ||
+ | use Symfony\Component\Process\Process; | ||
+ | |||
+ | $process = new class extends Process { | ||
+ | public function start() { | ||
+ | | ||
+ | } | ||
+ | }; | ||
+ | </code> | ||
+ | instead of the following: | ||
+ | < | ||
+ | namespace My\Namespace\Process; | ||
+ | |||
+ | use Symfony\Component\Process\Process as Base; | ||
+ | |||
+ | class Process extends Base { | ||
+ | public function start() { | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | |||
+ | $process = new \My\Namespace\Process\Process; | ||
+ | </ | ||
+ | |||
+ | ===== Backward Incompatible Changes ===== | ||
+ | |||
+ | New syntax that will fail to parse in previous versions, so no BC breaks. | ||
+ | |||
+ | ===== Proposed PHP Version(s) ===== | ||
+ | |||
+ | 7.0 | ||
+ | |||
+ | ===== SAPIs Impacted ===== | ||
+ | |||
+ | All | ||
+ | |||
+ | ===== Impact to Existing Extensions ===== | ||
+ | |||
+ | No impact on existing libraries | ||
+ | |||
+ | ===== Open Issues ===== | ||
+ | |||
+ | None | ||
+ | |||
+ | ===== Future Scope ===== | ||
+ | |||
+ | The changes made by this patch mean named nested classes are easier to implement (by a tiny bit). | ||
===== References ===== | ===== References ===== | ||
PHP 7 Discussion: http:// | PHP 7 Discussion: http:// | ||
+ | |||
+ | ===== Proposed Voting Choices ===== | ||
+ | |||
+ | The voting choices are yes (in favor for accepting this RFC for PHP 7) or no (against it). | ||
+ | |||
+ | ===== Vote ===== | ||
+ | |||
+ | Vote starts on March 13th, and will end two weeks later, on March 27th. | ||
+ | |||
+ | This RFC requires a 2/3 majority. | ||
+ | |||
+ | <doodle title=" | ||
+ | * Yes | ||
+ | * No | ||
+ | </ | ||
===== Changelog ===== | ===== Changelog ===== | ||
+ | * v0.6 Serialization not supported and names added | ||
+ | * v0.5.2 Updated name examples | ||
+ | * v0.5.1 Example improvements | ||
* v0.5: Added traits example and tests | * v0.5: Added traits example and tests | ||
* v0.4: Comparisons example | * v0.4: Comparisons example | ||
* v0.3: ReflectionClass:: | * v0.3: ReflectionClass:: | ||
* v0.2: Brought back for discussion | * v0.2: Brought back for discussion | ||
- | * v0.1: Initial | + | * v0.1: Initial |
+ | |||
+ | ===== Implementation ===== | ||
+ | |||
+ | https:// |
rfc/anonymous_classes.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1