|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2015-12-05 08:58 UTC] andreas at dqxtech dot net
Description: ------------ The native Reflection API should provide interfaces like \IReflectionClass, which can then be used for type hinting. This way, libraries such as https://github.com/Roave/BetterReflection or https://github.com/Andrewsville/PHP-Token-Reflection don't need to extend or wrap core reflection classes, but can instead implement the interface. One problem with this is that some libraries might only want to implement some of the methods of e.g. \ReflectionFunction, but not all of them. To allow this, there would need to be partial interfaces - which could be a longer discussion. I think for a start it would be sufficient to just have one interface for each of the already existing classes of the Reflection API. More interfaces could be added in the future. Test script: --------------- class C {..} function foo(\IReflectionClass $reflectionClass) {..} foo(new \ReflectionClass('C')); class MyReflectionClass implements \IReflectionClass { .. } foo(new MyReflectionClass('C')); PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 10:00:01 2025 UTC |
You almost certainly don't want this. Having interface in PHP core is fine for small well defined constructs. However it becomes very very painful when the interfaces are either only vaguely defined or very large, as each change to the definition of the interface would be a backwards compatibility breaking change. The Reflection classes are both large and ill-defined. Having the internal classes implement an interface would either be very annoying for users of this interface, or for PHP core devs who would now have to wait until major/minor releases to change the definitions of the interfaces. Alternatively, you can achieve what you want to achieve by simply defining the appropriate interface in UserLand, and then making a proxy implementation to the Reflection api. e.g. interface ReflectionClassInterface { public function getConstant(string $name); public function getConstants() : array; public function getConstructor() : \ReflectionMethod; //all the other methods. } class ReflectionClassCore implments ReflectionClassInterface { private $reflectionClass; public function __construct(\ReflectionClass $reflectionClass) { $this->reflectionClass = $reflectionClass; } public function getConstant(string $name) { return $this->reflectionClass->getConstant($name); } public function getConstants() : array { return $this->reflectionClass->getConstants(); } public function getConstructor() : \ReflectionMethod { return $this->reflectionClass->getConstructor(); } //...and so on. } This would take you less than an hour to do, which is significantly less time that just the discussion on the internals list would take. That interface wouldn't break when the internal classes have methods added or altered, and probably has other benefits as well e.g. being able to improve the reflection api without waiting for PHP core to change. TL:DR interfaces are great, but not right here....