[RFC] Anonymous Classes

php.internals

Philip Sturgeon

11 years ago
Good day! https://wiki.php.net/rfc/anonymous_classes There's a little RFC + patch that Joe Watkins put together, and as before with the ArrayOf RFC, I'll be helping out. So, lets get this discussion rolling. It was declined for PHP 5 some time ago, and has returned now to try for PHP 7. The usage of anonymous classes to some will be instantly offensive, but really it comes down to the use case. The usage of anonymous functions compared to declared functions is pretty much the exact same thing as anonymous classes. Other than examples on the RFC, Fractal would certainly be happy to have them: http://fractal.thephpleague.com/transformers/ Defining a class properly is certainly still going to be the majority of uses of classes in PHP. That helps with the Optimizer, and helps code reuse. Sadly due to the way in which people have had ONE CLASS ONE FILE drilled into their head since PEAR and continuing through Zend and PSR-0, it can become a PITA to add some simple functionality if a small class is needed for one tiny thing. Anonymous functions alleviate that annoyance with a simple and consistent feature that just give people a nice simple option to get their jobs done, without hitting autoloaders and file systems to do it.

Dmitry Stogov

11 years ago
I think the proposal is a bit incomplete. It's possible to instantiate an anonymous class, but currently it's not possible to do with them anything else (assign to variable, pass to function, etc). Something similar to Closure objects should be introduced. Thanks. Dmitry. On Tue, Feb 24, 2015 at 4:52 PM, Philip Sturgeon <pjsturgeon@gmail.com> wrote:

Philip Sturgeon

11 years ago
On Tue, Feb 24, 2015 at 9:10 AM, Dmitry Stogov <dmitry@zend.com> wrote:
> I think the proposal is a bit incomplete. > It's possible to instantiate an anonymous class, but currently it's not > possible to do with them anything else (assign to variable, pass to > function, etc). Something similar to Closure objects should be introduced. > > Thanks. Dmitry. > > On Tue, Feb 24, 2015 at 4:52 PM, Philip Sturgeon <pjsturgeon@gmail.com> > wrote:
1. You can absolutely assign the instantiated classes to variables. Check out this test in the patch: https://github.com/krakjoe/php-src/compare/anon#diff-25e330fb5a98810de178a5b798102d01R1 2. Why do you say they cannot be passed to a function? I can add a test if you can give me an example of what you're suggesting doesn't work. 3. Not sure why we'd need a Closure-alike object. Anonymous classes are just a class, and classes have all the types and hinting functionality of regular classes. You don't need to implement a class to let people know its a class. Maybe you could expand on that a bit? :)

Dennis Birkholz

11 years ago
Hi Phil, Am 24.02.2015 um 14:52 schrieb Philip Sturgeon:
> https://wiki.php.net/rfc/anonymous_classes
I like the idea of having anonymous classes, it is very helpful during development to just try something out without having the burden of creating a new file and a complete class including namespace and use declarations, etc. A great feature of anonymous classes in Java is their ability to access the private/protected properties of the object they are defined in, similar to what Closures do in PHP. The thing is, in Java if you access a variable (without this.), it may be of the current scope, a member of the current class or a member of the class where the anonymous class was defined in. In PHP, you have to use $this-> to access class members of the anonymous class but there is some $outer-> or similar variable missing for the access. You can as a work around pass all members by reference via the constructor, but that is really not nice. In addition, if you define an anonymous class inside a "real" class and pass another instance of the outer class, the anonymous class can still access the private/protected methods imho. Any thoughts how to handle this or should we just ignore it for now? Second thing is serialization. PHP closures can not be serialized, does the same apply for anonymous classes? It would be really nice if such anonymous classes could e.g. send via a command bus or something to be executed on another host. Thanks Dennis P.s.: there is a footnote in the RFC that does not belong there is think

Florian Anderiasch

11 years ago
On 02/24/2015 03:29 PM, Dennis Birkholz wrote:
> Am 24.02.2015 um 14:52 schrieb Philip Sturgeon: >> https://wiki.php.net/rfc/anonymous_classes > > I like the idea of having anonymous classes, it is very helpful during > development to just try something out without having the burden of > creating a new file and a complete class including namespace and use > declarations, etc.
I don't buy this argument. If your class loader or PSR prevents you from temporarily adding a 2nd bogus class to the same file (not talking about best practices here, obviously) then there's the culprit and it's nothing the language needs to fix. ~Florian

Philip Sturgeon

11 years ago
On Tue, Feb 24, 2015 at 9:58 AM, Florian Anderiasch <ml@anderiasch.de> wrote:
> On 02/24/2015 03:29 PM, Dennis Birkholz wrote: >> Am 24.02.2015 um 14:52 schrieb Philip Sturgeon: >>> https://wiki.php.net/rfc/anonymous_classes >> >> I like the idea of having anonymous classes, it is very helpful during >> development to just try something out without having the burden of >> creating a new file and a complete class including namespace and use >> declarations, etc. > > I don't buy this argument. If your class loader or PSR prevents you from > temporarily adding a 2nd bogus class to the same file (not talking about > best practices here, obviously) then there's the culprit and it's > nothing the language needs to fix. > > ~Florian > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php >
Your reply seems to assume that avoiding rules in a PSR is the only benefit of the RFC, which is weird when there is a whole RFC full of benefits of this RFC. Also, how do you "Temporarily" add a 2nd bogus class to the same file? Also again, why is it bogus? This RFC allows people to use really simple classes, which implement various things, or provide simple decoration, or insanely easy stubbing, or all sorts of other functionality without having to define it way at the top of the file, or in another file, or whatever. Not just bogus junk, but actual real uses. My mention of PSR (and PEAR and Zend which you didn't mention) was purely a sidenote of "This is how the community is doing things" which is always relevant to mention, and not a complaint about PSRs. This was common practice for the preceding decade, and again is not the crux of this conversation.

Florian Anderiasch

11 years ago
On 02/24/2015 04:31 PM, Philip Sturgeon wrote:
> On Tue, Feb 24, 2015 at 9:58 AM, Florian Anderiasch <ml@anderiasch.de> wrote: >> On 02/24/2015 03:29 PM, Dennis Birkholz wrote: >>> Am 24.02.2015 um 14:52 schrieb Philip Sturgeon: >>>> https://wiki.php.net/rfc/anonymous_classes >>> >>> I like the idea of having anonymous classes, it is very helpful during >>> development to just try something out without having the burden of >>> creating a new file and a complete class including namespace and use >>> declarations, etc. >> >> I don't buy this argument. If your class loader or PSR prevents you from >> temporarily adding a 2nd bogus class to the same file (not talking about >> best practices here, obviously) then there's the culprit and it's >> nothing the language needs to fix. >> > > Your reply seems to assume that avoiding rules in a PSR is the only > benefit of the RFC, which is weird when there is a whole RFC full of > benefits of this RFC.
My reply was simply re: the quoted paragraph in the mail I directly replied to, not yours. I simply think "trying something out" is not a valid use case of anonymous classes. I can generally like the RFC and still refute arguments in favor of it, right? :) ~Florian

Philip Sturgeon

11 years ago
On Tue, Feb 24, 2015 at 10:59 AM, Florian Anderiasch <ml@anderiasch.de> wrote:
> On 02/24/2015 04:31 PM, Philip Sturgeon wrote: >> On Tue, Feb 24, 2015 at 9:58 AM, Florian Anderiasch <ml@anderiasch.de> wrote: >>> On 02/24/2015 03:29 PM, Dennis Birkholz wrote: >>>> Am 24.02.2015 um 14:52 schrieb Philip Sturgeon: >>>>> https://wiki.php.net/rfc/anonymous_classes >>>> >>>> I like the idea of having anonymous classes, it is very helpful during >>>> development to just try something out without having the burden of >>>> creating a new file and a complete class including namespace and use >>>> declarations, etc. >>> >>> I don't buy this argument. If your class loader or PSR prevents you from >>> temporarily adding a 2nd bogus class to the same file (not talking about >>> best practices here, obviously) then there's the culprit and it's >>> nothing the language needs to fix. >>> >> >> Your reply seems to assume that avoiding rules in a PSR is the only >> benefit of the RFC, which is weird when there is a whole RFC full of >> benefits of this RFC. > > My reply was simply re: the quoted paragraph in the mail I directly > replied to, not yours. I simply think "trying something out" is not a > valid use case of anonymous classes. > > I can generally like the RFC and still refute arguments in favor of it, > right? :) > > ~Florian
I gotcha. Again though, Dennis wasn't saying "trying something out" is the only benefit, just the one. We can take that argument and shove it under the rug for sure, but it's perfectly reasonable for me as one of many arguments for it.

Stas Malyshev

11 years ago
Hi!
> I like the idea of having anonymous classes, it is very helpful during > development to just try something out without having the burden of > creating a new file and a complete class including namespace and use > declarations, etc.
I think this particular argument is a bit backwards. In PHP, you certainly don't need to create new file to just introduce a new class. If you're working within a framework that makes it a problem, time to think if the framework you're using is fit for your purposes. But PHP certainly does not impose such limitations.
> A great feature of anonymous classes in Java is their ability to access > the private/protected properties of the object they are defined in, > similar to what Closures do in PHP. The thing is, in Java if you access > a variable (without this.), it may be of the current scope, a member of > the current class or a member of the class where the anonymous class was > defined in. In PHP, you have to use $this-> to access class members of
That may be a problem to implement, exactly since you need $this in PHP.
> Second thing is serialization. PHP closures can not be serialized, does > the same apply for anonymous classes? It would be really nice if such
Serializing is not a problem, unserializing would be - as there may be no class for this object to instantiate.
-- Stas Malyshev smalyshev@gmail.com

Philip Sturgeon

11 years ago
On Tue, Feb 24, 2015 at 2:25 PM, Stanislav Malyshev <smalyshev@gmail.com> wrote:
> Hi! > >> I like the idea of having anonymous classes, it is very helpful during >> development to just try something out without having the burden of >> creating a new file and a complete class including namespace and use >> declarations, etc. > > I think this particular argument is a bit backwards. In PHP, you > certainly don't need to create new file to just introduce a new class. > If you're working within a framework that makes it a problem, time to > think if the framework you're using is fit for your purposes. But PHP > certainly does not impose such limitations. > >> A great feature of anonymous classes in Java is their ability to access >> the private/protected properties of the object they are defined in, >> similar to what Closures do in PHP. The thing is, in Java if you access >> a variable (without this.), it may be of the current scope, a member of >> the current class or a member of the class where the anonymous class was >> defined in. In PHP, you have to use $this-> to access class members of > > That may be a problem to implement, exactly since you need $this in PHP. > >> Second thing is serialization. PHP closures can not be serialized, does >> the same apply for anonymous classes? It would be really nice if such > > Serializing is not a problem, unserializing would be - as there may be > no class for this object to instantiate. > > -- > Stas Malyshev > smalyshev@gmail.com > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php >
If anyone wants to have a play with anonymous classes, our friends over at 3v4l.org have made this incredibly easy to do: http://3v4l.org/elh6I/rfc#rfc-anonymous_classes Awesome.

Matthew Leverton

11 years ago
On Tue, Feb 24, 2015 at 7:52 AM, Philip Sturgeon <pjsturgeon@gmail.com> wrote:
> Good day! > > https://wiki.php.net/rfc/anonymous_classes > > There's a little RFC + patch that Joe Watkins put together, and as > before with the ArrayOf RFC, I'll be helping out. > > So, lets get this discussion rolling. >
Anonymous classes open up slightly new ways to do things. I like to write a lot of small classes for the purposes of DI and re-usability. Because of that, the code often looks like: $foo = new Foo($depend1, $depend2, function() { return 'my custom behavior'; }); That's fine when there's only one callback needed, but beyond that it breaks down. This becomes more readable: abstract class Foo { ... } $foo = new class($depend1, $depend2) extends Foo { public function doFoo() { } public function doBar() { } } These cases for me are always single-use for the scope of the application, and often it's inside configuration blocks that are custom per environment. I have no need to name the class other than PHP doesn't support anonymous ones. I'm +1 on the concept, depending on what the maintenance impact that this would bring to PHP's core. There are enough other ways to accomplish the same goals that I don't think this is "must have", even though I'd definitely use it. Regarding this syntax (from other discussions on the list): $foo = class { }; I think if such a thing were supported, maybe $foo would just be a ReflectionClass object. (I haven't really thought that one through...)
-- Matthew Leverton

Unnamed Person

11 years ago
here is an example for opening a mysqli connection only when the first query is executed: $c = new class extends mysqli { public function query($query, $resultmode = MYSQLI_STORE_RESULT) { if (empty($this->host_info)) parent::real_connect('127.0.0.1', 'user', 'pwd', 'db'); return parent::query($query, $resultmode); } } // do sth for some time $->query('...'); // triggers real_connect $->query('...'); // connection is already established I haven't tested this, so I'm only intending to present the idea. Regards Thomas Philip Sturgeon wrote on 24.02.2015 14:52:

Mike Willbanks

11 years ago
On Tue, Feb 24, 2015 at 7:52 AM, Philip Sturgeon <pjsturgeon@gmail.com> wrote:
> Good day! > > https://wiki.php.net/rfc/anonymous_classes > > There's a little RFC + patch that Joe Watkins put together, and as > before with the ArrayOf RFC, I'll be helping out. > > So, lets get this discussion rolling. > > It was declined for PHP 5 some time ago, and has returned now to try for > PHP 7. > > The usage of anonymous classes to some will be instantly offensive, > but really it comes down to the use case. The usage of anonymous > functions compared to declared functions is pretty much the exact same > thing as anonymous classes. > > Other than examples on the RFC, Fractal would certainly be happy to > have them: http://fractal.thephpleague.com/transformers/
I would also find myself to be a very happy consumer of this. It can provide the opportunity of getting rid of a large amount of boilerplate. In addition, there has been several times when implementing single use extensions for say view libraries (Plates extensions comes to mind) or Plugin handling for things like Zend Framework 2 and there has been several times I utilize a closure with an array to get around having an object simply due to you want to have a callback but with some form of definition. I think there is a very large use case here and something that would be a great addition to the language. Sure, I could make a class and extend it just like this would do but for the convenience of the implementation it is sometimes better to do a simple closure. Defining a class properly is certainly still going to be the majority
> of uses of classes in PHP. That helps with the Optimizer, and helps > code reuse. > > Sadly due to the way in which people have had ONE CLASS ONE FILE > drilled into their head since PEAR and continuing through Zend and > PSR-0, it can become a PITA to add some simple functionality if a > small class is needed for one tiny thing. >
I see the point here, but the argument to me seems void. Nothing wrong with a simple class one file for a small class.

Philip Sturgeon

11 years ago
On Tue, Feb 24, 2015 at 12:21 PM, Mike Willbanks <pencap@gmail.com> wrote:
> > > On Tue, Feb 24, 2015 at 7:52 AM, Philip Sturgeon <pjsturgeon@gmail.com> > wrote: >> >> Good day! >> >> https://wiki.php.net/rfc/anonymous_classes >> >> There's a little RFC + patch that Joe Watkins put together, and as >> before with the ArrayOf RFC, I'll be helping out. >> >> So, lets get this discussion rolling. >> >> It was declined for PHP 5 some time ago, and has returned now to try for >> PHP 7. >> >> The usage of anonymous classes to some will be instantly offensive, >> but really it comes down to the use case. The usage of anonymous >> functions compared to declared functions is pretty much the exact same >> thing as anonymous classes. >> >> Other than examples on the RFC, Fractal would certainly be happy to >> have them: http://fractal.thephpleague.com/transformers/ > > > I would also find myself to be a very happy consumer of this. It can > provide the opportunity of getting rid of a large amount of boilerplate. In > addition, there has been several times when implementing single use > extensions for say view libraries (Plates extensions comes to mind) or > Plugin handling for things like Zend Framework 2 and there has been several > times I utilize a closure with an array to get around having an object > simply due to you want to have a callback but with some form of definition. > I think there is a very large use case here and something that would be a > great addition to the language. Sure, I could make a class and extend it > just like this would do but for the convenience of the implementation it is > sometimes better to do a simple closure. > >> Defining a class properly is certainly still going to be the majority >> of uses of classes in PHP. That helps with the Optimizer, and helps >> code reuse. >> >> Sadly due to the way in which people have had ONE CLASS ONE FILE >> drilled into their head since PEAR and continuing through Zend and >> PSR-0, it can become a PITA to add some simple functionality if a >> small class is needed for one tiny thing. > > > I see the point here, but the argument to me seems void. Nothing wrong with > a simple class one file for a small class. >
Something doesn't have to be "wrong", another approach can just be more convenient, or handy for different use cases. One class one file can be rather slow depending on the autoloader setup, and depending on whether zend optimizer is enabled or not. For a distributed application running on mostly crap shared hosting, relying on Zend Optimizer is not an option, so the more of these little one-class one-file situations you have the worse it becomes. This RFC offers a solution for people that is not "Jam a bunch of these classes at the top of the current file" or "Make a whole load of one-time use classes to avoid that mess, plus people will snort and shame you for failing to follow decade old best practices."

Alex Sky

11 years ago
Would have been more useful, inline sintex like JSON: $object = { 'property': $value, 'method': function (){...} }; $object->property; $object->method();

Philip Sturgeon

11 years ago
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.

Alex Sky

11 years ago
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.

Yasuo Ohgaki

11 years ago
Hi all, On Wed, Feb 25, 2015 at 4:06 AM, S.A.N <ua.san.alex@gmail.com> wrote:
> 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. >
It's not directly related to the topic, but I would like to have "code block" that returns "value" with this syntax rather than defining anonymous class/object. BTW, I'm +1 for the idea having anonymous class. Regards,
-- Yasuo Ohgaki yohgaki@ohgaki.net

Unnamed Person

11 years ago
I'm not sure if json syntax is better than PHP here: $object = (object)[ 'property'=>$value, 'method'=>function(){...} ]; $object->property; $object->method->__invoke(); S.A.N wrote on 24.02.2015 19:37:

Stas Malyshev

11 years ago
Hi!
> https://wiki.php.net/rfc/anonymous_classes > > There's a little RFC + patch that Joe Watkins put together, and as > before with the ArrayOf RFC, I'll be helping out. > > So, lets get this discussion rolling.
This is a nice and clear proposal, however I'm not sure I'm completely convinced about the necessity of it. In Java this kind of pattern is used very frequently, because Java is a statically typed language where it is the only way to achieve dynamic interface, and has other capabilities, see below. In PHP, there are other ways (and can be made more ways, such as more support for duck typing). Also, the question arises when comparing this to Java - would anonymous class functions be closures connected to the environment? In Java, they are and that is what they are most frequently used for - to make contextualized callbacks. In PHP, you need special syntax to capture the environment, so how that would work? Also, what about scope access, etc.?
> Sadly due to the way in which people have had ONE CLASS ONE FILE > drilled into their head since PEAR and continuing through Zend and > PSR-0, it can become a PITA to add some simple functionality if a > small class is needed for one tiny thing.
One class/one file may be a good idea in general for a big project, but as they say, any feature that can not be turned off when needed is a bug.
-- Stas Malyshev smalyshev@gmail.com

Robert Stoll

11 years ago
Hi Philip
> -----Ursprüngliche Nachricht----- > Von: Philip Sturgeon [mailto:pjsturgeon@gmail.com] > Gesendet: Dienstag, 24. Februar 2015 14:52 > An: PHP Internals > Betreff: [PHP-DEV] [RFC] Anonymous Classes > > Good day! > > https://wiki.php.net/rfc/anonymous_classes > > There's a little RFC + patch that Joe Watkins put together, and as before with the ArrayOf RFC, I'll be helping out. >
[Robert Stoll] I like the RFC +1 :) Can I also use traits in combination with anonymous classes?=3D20

Philip Sturgeon

11 years ago
On Tue, Mar 3, 2015 at 12:03 PM, Robert Stoll <php@tutteli.ch> wrote:
> Hi Philip > >> -----Ursprüngliche Nachricht----- >> Von: Philip Sturgeon [mailto:pjsturgeon@gmail.com] >> Gesendet: Dienstag, 24. Februar 2015 14:52 >> An: PHP Internals >> Betreff: [PHP-DEV] [RFC] Anonymous Classes >> >> Good day! >> >> https://wiki.php.net/rfc/anonymous_classes >> >> There's a little RFC + patch that Joe Watkins put together, and as before with the ArrayOf RFC, I'll be helping out. >> > > [Robert Stoll] > I like the RFC +1 :) > > Can I also use traits in combination with anonymous classes?=3D20 >
You can indeed use traits. Should I add this as an example and shove a test in there?

Robert Stoll

11 years ago
> -----Ursprüngliche Nachricht----- > Von: Philip Sturgeon [mailto:pjsturgeon@gmail.com] > Gesendet: Mittwoch, 4. März 2015 16:49 > An: Robert Stoll > Cc: PHP Internals > Betreff: Re: [PHP-DEV] [RFC] Anonymous Classes > > On Tue, Mar 3, 2015 at 12:03 PM, Robert Stoll <php@tutteli.ch> wrote: > > Hi Philip > > > >> -----Ursprüngliche Nachricht----- > >> Von: Philip Sturgeon [mailto:pjsturgeon@gmail.com] > >> Gesendet: Dienstag, 24. Februar 2015 14:52 > >> An: PHP Internals > >> Betreff: [PHP-DEV] [RFC] Anonymous Classes > >> > >> Good day! > >> > >> https://wiki.php.net/rfc/anonymous_classes > >> > >> There's a little RFC + patch that Joe Watkins put together, and as before with the ArrayOf RFC, I'll be helping out. > >> > > > > [Robert Stoll] > > I like the RFC +1 :) > > > > Can I also use traits in combination with anonymous classes?=3D20 > > > > You can indeed use traits. Should I add this as an example and shove a test in there? > > -- > PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
I would, maybe I oversaw it but I was not sure if it is possible after reading the description of your RFC.

Philip Sturgeon

11 years ago
Right, this here RFC has been drastically improved. https://wiki.php.net/rfc/anonymous_classes Anyone got any doubts or troubles at this point? It's about 5 days until the vote starts.

Stas Malyshev

11 years ago
Hi!
> Right, this here RFC has been drastically improved. > > https://wiki.php.net/rfc/anonymous_classes > > Anyone got any doubts or troubles at this point? > > It's about 5 days until the vote starts.
Couple of points to clarify: 1. Is the new syntax "new class ..." or just "class ..." and new works the same? I.e. could you do: $foo = class extends Callback { ... } $bar = new $foo; 2. What is the scope of the anonymous class? Can it get access to parent's variable scope? Private functions?
-- Stas Malyshev smalyshev@gmail.com

Philip Sturgeon

11 years ago
Stanislav, On Fri, Mar 6, 2015 at 6:42 PM, Stanislav Malyshev <smalyshev@gmail.com> wrote:
> Hi! > Couple of points to clarify: > > 1. Is the new syntax "new class ..." or just "class ..." and new works > the same? I.e. could you do: > > $foo = class extends Callback { ... } > $bar = new $foo;
Nope, none of that. Wrap that in a function or clone it perhaps.
> 2. What is the scope of the anonymous class? Can it get access to > parent's variable scope? Private functions?
Works the same as any named class. No new special rules here. Play around on 3v4l and show me an example of something you don't like, or make sure it works the way you expect.

Patrick Schaaf

11 years ago
Am 06.03.2015 20:14 schrieb "Philip Sturgeon" <pjsturgeon@gmail.com>:
> > Right, this here RFC has been drastically improved. > > https://wiki.php.net/rfc/anonymous_classes > > Anyone got any doubts or troubles at this point?
Can we / could we do "extends self", "extends static", or even "extends $someclassname" ? The first two could be handy in factory like object methods, and the last one generally in factory methods. best regards Patrick

Lazare Inepologlou

11 years ago
2015-03-07 8:22 GMT+01:00 Patrick Schaaf <php@bof.de>:
> Am 06.03.2015 20:14 schrieb "Philip Sturgeon" <pjsturgeon@gmail.com>: > > > > Right, this here RFC has been drastically improved. > > > > https://wiki.php.net/rfc/anonymous_classes > > > > Anyone got any doubts or troubles at this point? > > Can we / could we do "extends self", "extends static", or even "extends > $someclassname" ? > >
+1 Lazare INEPOLOGLOU Ingénieur Logiciel

Robert Stoll

11 years ago
> -----Ursprüngliche Nachricht----- > Von: Patrick Schaaf [mailto:php@bof.de] > Gesendet: Samstag, 7. März 2015 08:22 > An: Philip Sturgeon > Cc: internals; Robert Stoll > Betreff: Re: [PHP-DEV] [RFC] Anonymous Classes > > Am 06.03.2015 20:14 schrieb "Philip Sturgeon" <pjsturgeon@gmail.com>: > > > > Right, this here RFC has been drastically improved. > > > > https://wiki.php.net/rfc/anonymous_classes > > > > Anyone got any doubts or troubles at this point? > > Can we / could we do "extends self", "extends static", or even "extends $someclassname" ? > > The first two could be handy in factory like object methods, and the last one generally in factory methods. > > best regards > Patrick
Personally, I would go step by step. First introduce anonymous classes and then extend it with features like the one described above - should be less controversial. The last construct idea described by you would introduce dynamic inheritance to a certain degree and I am not so sure if that is really a good idea Cheers, Robert

Marcio Almada

11 years ago
Hi, 2015-03-07 8:28 GMT-03:00 Robert Stoll <php@tutteli.ch>:
> > > -----Ursprüngliche Nachricht----- > > Von: Patrick Schaaf [mailto:php@bof.de] > > Gesendet: Samstag, 7. März 2015 08:22 > > An: Philip Sturgeon > > Cc: internals; Robert Stoll > > Betreff: Re: [PHP-DEV] [RFC] Anonymous Classes > > > > Am 06.03.2015 20:14 schrieb "Philip Sturgeon" <pjsturgeon@gmail.com>: > > > > > > Right, this here RFC has been drastically improved. > > > > > > https://wiki.php.net/rfc/anonymous_classes > > > > > > Anyone got any doubts or troubles at this point? > > > > Can we / could we do "extends self", "extends static", or even "extends > $someclassname" ? > > > > The first two could be handy in factory like object methods, and the > last one generally in factory methods. > > > > best regards > > Patrick > > Personally, I would go step by step. First introduce anonymous classes and > then extend it with features like the one described above - should be less > controversial. > The last construct idea described by you would introduce dynamic > inheritance to a certain degree and I am not so sure if that is really a > good idea > > Cheers, > Robert > > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > >
That's not controversial, it seems like a basic feature we should have. As anonymous classes is a major feature, adding "extends self" and "extends static" at a later stage would only cause portability issues between different versions of PHP7. If we are going to have this, better add it now. +1 Cheers, Márcio

Philip Sturgeon

11 years ago
Patrick, On Sat, Mar 7, 2015 at 2:22 AM, Patrick Schaaf <php@bof.de> wrote:
> > Am 06.03.2015 20:14 schrieb "Philip Sturgeon" <pjsturgeon@gmail.com>: >> >> Right, this here RFC has been drastically improved. >> >> https://wiki.php.net/rfc/anonymous_classes >> >> Anyone got any doubts or troubles at this point? > > Can we / could we do "extends self", "extends static", or even "extends > $someclassname" ? > > The first two could be handy in factory like object methods, and the last > one generally in factory methods. > > best regards > Patrick
Could you maybe make some full examples? I'm not visualising the use case. It sounds like something which would be better off as another RFC as its extra features. This is just making anonymous definitions possible, not trying to take on a whole bunch of different features in one go.

Unnamed Person

11 years ago
Just a question, can I do: $c = class { public static function square($i) { return $i*$i; } }); echo $c::square(42); or $c = class extends DateTime { public static function createFromFormat($translateFormat, $time) { $translateFormat = str_replace('foo', 'Y-m-d', $translateFormat); return parent::createFromFormat($translateFormat, $time); } }); $date = $c::createFromFormat('foo', '2015-03-09'); Regards Thomas Philip Sturgeon wrote on 06.03.2015 20:14:

Lee davis

11 years ago
Hi Phil. I really like this feature and am keen to see it go in. Developers often create classes (which are typically new files) just to extend and implement a small method. This feature could see codebases become much lighter. So a massive thanks for your and Joe’s efforts on this. I have two quite small points I want to make. Firstly, When a error occurs on an anonymous class the class description isn’t very useful (see http://3v4l.org/9Nbea/rfc#tabs). Instead of “class@0x7f9e33870fd0” Is it possible it could be prefixed with “anonymous” or something a bit clearer. Further to this if the anonymous class extends anything it omits the parent class name in the error message (see http://3v4l.org/UFEbo/rfc#tabs). Is this expected? Could it be something more helpful? Secondly, I think we need to either allow the behaviour or generate an error (similar to anonymous closures) when an anonymous class is serialized. Please observe the following behaviour.. $foo = new class { public function Bar () { echo 'bar'; } }; // This succeeds. I'd argue an error should be generated at this point echo $serFoo = serialize($foo); // Unserialize fails and a notice is generated $unFoo = unserialize($serFoo); // Error generated on call $unFoo->Bar(); see http://3v4l.org/AJY6d/rfc#tabs That’s all. Again, thanks for your efforts. /@leedavis81 On Tue, Feb 24, 2015 at 1:52 PM, Philip Sturgeon <pjsturgeon@gmail.com> wrote: