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.
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:
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.
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:
- 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
-
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. -
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? :)
Hi Phil,
Am 24.02.2015 um 14:52 schrieb Philip Sturgeon:
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
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
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 ofThat 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 suchSerializing is not a problem, unserializing would be - as there may be
no class for this object to instantiate.--
Stas Malyshev
smalyshev@gmail.com--
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.
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
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:
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.
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.
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.
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."
Would have been more useful, inline sintex like JSON:
$object =
{
'property': $value,
'method': function (){...}
};
$object->property;
$object->method();
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.
2015-02-24 20:49 GMT+02:00 Philip Sturgeon pjsturgeon@gmail.com:
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.
Hi all,
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
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:
Would have been more useful, inline sintex like JSON:
$object =
{
'property': $value,
'method': function (){...}
};$object->property;
$object->method();
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
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 ClassesGood 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
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 ClassesGood 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?
-----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 ClassesHi 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 ClassesGood 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?
--
I would, maybe I oversaw it but I was not sure if it is possible after reading the description of your RFC.
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.
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:
- 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;
- What is the scope of the anonymous class? Can it get access to
parent's variable scope? Private functions?
--
Stas Malyshev
smalyshev@gmail.com
Stanislav,
Hi!
Couple of points to clarify:
- 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.
- 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.
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
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
-----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 ClassesAm 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
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 ClassesAm 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
PatrickPersonally, 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 ideaCheers,
Robert--
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
Patrick,
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.
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:
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.
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:
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.