This is an old revision of the document!

PHP RFC: Anonymous Classes v2


We have looked at anonymous classes before http://wiki.php.net/rfc/anonymous_classes

The patch is only a tiny bit different, moving constructor arguments to the position where most expect them.

Many many use cases were put forward by the community for the last RFC and they all still apply.


Some example code …

class A {}
trait ArrayAccessor {
    public function offsetSet($offset, $data) { return $this->{$this->name}[$offset] = $data; }
    public function offsetGet($offset)        { return $this->{$this->name}[$offset]; }
    public function offsetUnset($offset)      { return $this->{$this->name}[$offset]; }
    public function offsetExists($offset)     { return isset($this->{$this->name}[$offset]); }
    public function setArray($name, $member) {
        $this->name = $name;
            {$this->name} = $member;
    private $name;
$test = new class(["hello", "world"])
            extends A implements ArrayAccess {
    use ArrayAccessor;
    public function __construct($greeting) {
        $this->setArray("data", $greeting);
    protected $data;
         $test instanceof ArrayAccess,
         $test instanceof A,
         $test[0], $test[1]);

Detail how anonymous classes can be used to make better use of traits here …

Detail other things here …

Proposed PHP Version(s)


RFC Impact

To Existing Extensions

Existing extensions performing magic or analysis of opcodes may require revision.

To Opcache

Opcache might need some revision, and a new optimization may also be possible. Discussion with dmitry required.

Proposed Voting Choices

A straight yes/no vote should be conducted.

A 2/3 majority is required for the RFC to pass.

Patches and Tests


Links to external references, discussions or RFCs

Rejected Features

Keep this updated with features that were discussed on the mail lists.

rfc/anonymous_classes_v2.1413961364.txt.gz · Last modified: 2017/09/22 13:28 (external edit)