Bug #70106 [Com]: Inheritance by string, ReflectionClass and anonymous class
| From: | soevilcat at mail dot ru | Date: | Fri, 24 Jul 2015 10:36:56 +0000 |
| Subject: | Bug #70106 [Com]: Inheritance by string, ReflectionClass and anonymous class | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-194671@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=70106&edit=1
ID: 70106
Comment by: soevilcat at mail dot ru
Reported by: soevilcat at mail dot ru
Summary: Inheritance by string, ReflectionClass and anonymous
class
Status: Closed
Type: Bug
Package: Reflection related
Operating System: any
PHP Version: 7.0.0beta2
Assigned To: bwoebi
Block user comment: N
Private report: N
New Comment:
I made a proof-of-concept repo for a kind of framework I mentioned.
https://github.com/Katemare/ClassClosure-proof-of-concept
Previous Comments:
------------------------------------------------------------------------
[2015-07-21 23:19:58] soevilcat at mail dot ru
I'll have to rethink it, of course... But they're only hacks because current PHP lacks
these patterns; or even only lacks syntax sugar for them, since technically it's absolutely
possible. Still, PHP has tendency to implement useful features from other languages, so maybe
someday. Thanks! :)
------------------------------------------------------------------------
[2015-07-21 21:11:11] bwoebi@php.net
Yeah, that is part of anon classes that the constructor is called upon their declaration. No way
around that. But really, just use an init() method or something along these lines if you need that.
And yes, that's the only issue, class_alias is persistent. So, yes, to use the name
dynamically, you'll have to recur to eval() with a generated unique name.
But I'd honestly rethink the application design if you need such hacks... ;-)
------------------------------------------------------------------------
[2015-07-21 20:30:08] soevilcat at mail dot ru
Thank you for quick response! A haven't realized that anonymous classes can be aliased (if
it's not mentioned in the docs yet, it's worth mentioning).
I see two problems, though... First of all, by returning an object belonging to an anonymous class
instead of a Closure-like representation of the class, you need to instantiate the class at least
once... It might not be optimal. Consider this example:
function get_base_animal_class() {
return new class('placeholder_name') {
public $name;
public __construct($name) { $this->name=$name; }
}
}
We have to force a placeholder name on the temporary instance, though from the logical standpoint
it's completely unnecessary. I know this is looked down upon in JavaScript where it was used to
be a pattern for inheriting.
Secondly, is class_alias() scoped? Or, maybe, an alias can be removed? (Sorry, I can't verify
this until tomorrow for PHP7 and I don't see it in docs.) If not, then after I use
"class_alias(get_class($proto), 'AnonBase')" in one place, I can't use the
same line in another place... I could alias to a different, generated class name each time, but I
still have to state "new class() extends SpecificName" unless I can use strings in
inheritance. I can still do this with eval()... But it's quite ugly.
------------------------------------------------------------------------
[2015-07-21 18:34:12] bwoebi@php.net
Automatic comment on behalf of bobwei9@hotmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=2141ab9be5c2b07c24534552753f8227473efa07
Log: Fix bug #70106 (Inheritance by anonymous class)
------------------------------------------------------------------------
[2015-07-21 18:33:19] bwoebi@php.net
You actually can do a workaround via
class_alias(get_class($the_anon_class_instance), "AnonBase");
and then do
new class extends AnonBase { ... }
See also http://3v4l.org/SKWdW
(Assigning to myself as there's a bug when anything in the file path is uppercase)
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=70106
--
Edit this bug report at https://bugs.php.net/bug.php?id=70106&edit=1