Bug #70106 [Asn->Csd]: Inheritance by string, ReflectionClass and anonymous class

From: Date: Tue, 21 Jul 2015 18:34:13 +0000
Subject: Bug #70106 [Asn->Csd]: Inheritance by string, ReflectionClass and anonymous class
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-194593@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 Updated by: bwoebi@php.net Reported by: soevilcat at mail dot ru Summary: Inheritance by string, ReflectionClass and anonymous class -Status: Assigned +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: 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) Previous Comments: ------------------------------------------------------------------------ [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) ------------------------------------------------------------------------ [2015-07-21 14:34:12] soevilcat at mail dot ru Description: ------------ Class inheritance is done in runtime, judging from how you can do: if (rand(0,1)==1) { class Lifeform extends Animal {}; } else { class Lifeform extends Plant {}; } You can even make it inherit class features by string by doing eval(). It would be much more convenient to have an official syntax for that, since using eval() has so many drawbacks - and is impossible with anonymous classes from PHP7 which have no name to reference. Jbviously they have _some_ internal handle... Actual use case: a framework designed to be able to run multiple copies of the same extension, but of different versions, for test purposes or otherwise (let's say, "Game 1.2" and "Game 1.3"). Files of each are located in different subfolders. The class/namespace structure would be almost identical, so all the classes are handled anonymously and accessed by factories or properties (much like can be done in Ruby or JavaScript). Another extension is desined to operate with "Game" extension - let's say, "SubGame". It was configured by admin to extend "Game 1.3" classes and not "Game 1.2" classes. Another "SubGame" instance could be configured to extend "Game 1.2". But for now, there's no way to inherit an anonymous class... The only solution would be to use tricky composition to emulate inheritance. Test script: --------------- // inheritance by string class Animal { public function eat() { echo 'Yummy!'; } } class Plant { public $size=0; public function grow() { echo 'I\'m level '.++$this->size; } } if (rand(0,1)==0) $base='Animal'; else $base='Plant'; class Lifeform extends $base { } // inheritance by ReflectionClass $reflection=new ReflectionClass('Animal'); class Predator extends $reflection->getClassEntry() { public $prey; } // inheritance by anonymous class; also requires an object representing the anonymous class, like Closure is for anonymous functions. function get_base_animal_class() { return class extends Animal { public $color; public function hiss() { echo 'Hiss!'; } }; } $tiger_class=class extends get_base_animal_class() { public $color='orange'; } Expected result: ---------------- No errors; valid syntax for inheriting classes. Actual result: -------------- Fatal errors: the suggested syntax in currently impossible. ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=70106&edit=1

« previous php.bugs (#194593) next »