rfc:anonymous_classes_v2
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:anonymous_classes_v2 [2014/10/22 07:02] – krakjoe | rfc:anonymous_classes_v2 [2017/09/22 13:28] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 9: | Line 9: | ||
We have looked at anonymous classes before http:// | We have looked at anonymous classes before http:// | ||
- | The patch is only a tiny bit different, moving constructor arguments to the position where most expect them. | + | There has been many requests from the community to incorporate this feature, |
- | Many many use cases were put forward by the community for the last RFC and they all still apply. | + | Follows is an edited version of that communication, |
===== Proposal ===== | ===== Proposal ===== | ||
- | Some example | + | In a unit test, test doubles (dummies, fakes, stubs, spys, and mocks) are used to simulate the behavior of complex, real objects when a real object is difficult or impossible to incorporate into a unit test. A test double can be used anywhere in the program where the program expects an object of the original class. |
+ | |||
+ | Traditionally, | ||
+ | |||
+ | Quoting Robert C. Martin: | ||
+ | |||
+ | Stubs and spies are very easy to write. My IDE makes it trivial. | ||
+ | I just point at the interface and tell the IDE to implement it. | ||
+ | Voila! It gives me a dummy. Then I just make a simple modification | ||
+ | and turn it into a stub or a spy. [...] | ||
+ | I don't like the strange syntax of mocking tools, and the | ||
+ | complications they add to my setups. I find writing my own test | ||
+ | doubles to be simpler in most cases. | ||
+ | |||
+ | The more I think about this (especially in the aftermath of the unserialize() issue), the more it appeals to me to have " | ||
+ | |||
+ | The question remains, though, where to put this code. Obviously it belongs with the test suite. But how to name the test double classes? Unique names are required to be able to have multiple (differently configured) test doubles for the same original class. Mocking tools (such as PHPUnit_MockObject, | ||
+ | |||
+ | Anonymous classes could be a solution for this problem. | ||
+ | |||
+ | This RFC proposes the following syntax for creating an object of an anonymous class, where the class does not extend from another and implements no interfaces. | ||
<code php> | <code php> | ||
- | <?php | + | $test = new class(" |
- | class A {} | + | public function __construct($greeting) { |
+ | $this-> | ||
+ | | ||
+ | }; | ||
+ | </ | ||
- | trait ArrayAccessor { | + | The normal rules of inheritance apply to anonymous classes, an anonymous class that inherits a parent would be declared as follows: |
- | public function offsetSet($offset, $data) { return $this-> | + | |
- | | + | <code php> |
- | | + | <?php |
- | | + | $test = new class("Hello World") |
- | + | | |
- | public function | + | public function |
- | $this->name = $name; | + | $this->greeting |
- | $this-> | + | |
- | {$this-> | + | |
} | } | ||
- | | + | }; |
- | | + | </ |
+ | |||
+ | An anonymous class, as any other class, can implement many interfaces as follows: | ||
+ | |||
+ | <code php> | ||
+ | <?php | ||
+ | interface SomeInterface { | ||
+ | | ||
} | } | ||
+ | $test = new class(" | ||
+ | extends AnotherClass implements SomeInterface { | ||
+ | public function __construct($greeting) { | ||
+ | $this-> | ||
+ | } | ||
+ | |||
+ | /* implement SomeInterface */ | ||
+ | public function method() {} | ||
+ | }; | ||
+ | </ | ||
+ | |||
+ | An anonymous class may also use traits, in the normal way: | ||
+ | |||
+ | <code php> | ||
+ | <?php | ||
$test = new class([" | $test = new class([" | ||
- | | + | implements ArrayAccess { |
+ | /* assume this trait implements ArrayAccess and contains ::setArray */ | ||
use ArrayAccessor; | use ArrayAccessor; | ||
| | ||
Line 46: | Line 91: | ||
protected $data; | protected $data; | ||
}; | }; | ||
- | |||
- | var_dump($test, | ||
- | $test instanceof ArrayAccess, | ||
- | $test instanceof A, | ||
- | | ||
- | | ||
?> | ?> | ||
</ | </ | ||
- | Detail how anonymous classes can be used to make better use of traits here ... | + | The ReflectionClass has been updated with ReflectionClass:: |
- | + | ||
- | Detail other things here ... | + | |
===== Proposed PHP Version(s) ===== | ===== Proposed PHP Version(s) ===== |
rfc/anonymous_classes_v2.1413961364.txt.gz · Last modified: 2017/09/22 13:28 (external edit)