Req #70106 [NEW]: Inheritance by string, ReflectionClass and anonymous class
| From: | soevilcat at mail dot ru | Date: | Tue, 21 Jul 2015 14:34:13 +0000 |
| Subject: | Req #70106 [NEW]: Inheritance by string, ReflectionClass and anonymous class | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-194590@lists.php.net to get a copy of this message | ||
From: soevilcat at mail dot ru
Operating system: any
PHP version: 7.0.0beta1
Package: Reflection related
Bug Type: Feature/Change Request
Bug description:Inheritance by string, ReflectionClass and anonymous class
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 bug report at https://bugs.php.net/bug.php?id=70106&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=70106&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=70106&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=70106&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=70106&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=70106&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=70106&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=70106&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=70106&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=70106&r=support
Expected behavior: https://bugs.php.net/fix.php?id=70106&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=70106&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=70106&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=70106&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=70106&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=70106&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=70106&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=70106&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=70106&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=70106&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=70106&r=mysqlcfg