Re: [RFC] Anonymous Classes

From: Date: Tue, 24 Feb 2015 19:06:20 +0000
Subject: Re: [RFC] Anonymous Classes
References: 1 2 3 4 5  Groups: php.internals 
Request: Send a blank email to internals+get-83683@lists.php.net to get a copy of this message
2015-02-24 20:49 GMT+02:00 Philip Sturgeon <pjsturgeon@gmail.com>: > On Tue, Feb 24, 2015 at 1:37 PM, S.A.N <ua.san.alex@gmail.com> wrote: >> Would have been more useful, inline sintex like JSON: >> >> $object = >> { >> 'property': $value, >> 'method': function (){...} >> }; >> >> $object->property; >> $object->method(); > > I know what you're saying here, and object literals like this would be > rather interesting someday. They are, however, quite different to the > concept of anonymous classes. > > JSON syntax would make it very hard for the usual PHP functionality to > be used. Properties and methods look the same, visibility scopes > cannot be used, implementing interfaces becomes impossible, traits > dont have a home, etc. By the time you have catered to all of that you > have something very different to JSON, and very different to PHP. > > Or, you have the syntax being proposed here, which is identical to > existing declared class syntax other than there being no name. > > Another RFC to replace $foo = (object) [ 'foo' => 'bar' ]; someday > would be nice, but that is not what this is up to. Yes, is problem - properties and methods look the same, how to solve this problem in PHP I do not know. Perhaps PHP could interpret this code: $object = { 'property' => $value, 'method' => function (){...} }; AS $object = new class { public $property = $value; public function method (){...} } Use case - inline object without inheritance and traits, only the properties and methods it brief and easily understood JSON syntax, I think it would be popular with developers of PHP. Thank.

« previous php.internals (#83683) next »