|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2019-08-21 16:40 UTC] 6562680 at gmail dot com
Description:
------------
I know about 7.4 and array-functions, seems like good solution.
So, currently i found 2 problems with Reflection
1. When you passing callable as string or array, you should be sure that passed arguments count will be EXACTLY match await arguments count. Maybe as solution, but \Closure::class works well. If you pass too many arguments, it just ignored. Because of that i should write bind() function that creates closure from these callable, controls bound arguments with \ReflectionClass
2. When i have few libraries with functions, sometimes i want call function that will be declared only when first access (because library has a constructor) by passing array inside mapper, filter or reducer. So i cant do this because library has another module inside (that works only with dependency injector) - so trying to pass array with that [ ProxyLib::class, 'func' ] expectedly ends with "method not found exception". Of course - if i use \Closure for that - it works well because of dynamic loading.
Test script:
---------------
/*
* 1st
*/
array_reduce($array, 'is_array', []); // nvm, just an example
// throws `Too many arguments`
/*
* 2nd
*/
Class Proxy
{
public static function __callStatic($method, $arguments)
{
// yes we should use DI or something here
return (new Dynamic)->{ $method }(...$arguments);
}
}
Class Dynamic
{
public function __construct() { $this->loadSomeDataOnInit(); }
public function reducer($carry, $item) { return $carry; }
}
array_reduce($array, [ Proxy::class, 'reducer' ], []);
// throws `Method not found`
array_reduce($array, function ($carry, $item) {
return Proxy::reducer($carry, $item); // works well, because of dynamic loading
}, []);
Expected result:
----------------
1st: ignore if too many, exception if not enough
2st: auto-wrap into closure
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Oct 25 23:00:01 2025 UTC |
<?php spl_autoload_register(function () { require 'dynamic.php'; }); Class Proxy { public static function __callStatic($method, $arguments) { // yes we should use DI or something here return (new Dynamic)->{ $method }(...$arguments); } } /* This class will be loaded via spl_autoload_register() and should be declared outside Class Dynamic { public function __construct() { // some code to load data outside } public function reducer($carry) // ! yes there should be 2 arguments { return $carry; // does nothing } } */ // run #2.1 var_dump(new \ReflectionMethod(Proxy::class, 'reducer')); // expected // throws `Method not found` // run #2.2 $array = [1,2,3]; array_reduce($array, [ Proxy::class, 'reducer' ], []); // sorry me, works well, i thought there should be `too many arguments` exception // run #2.3 array_reduce($array, function ($carry, $item) { return Proxy::reducer($carry, $item); // works well, because of dynamic loading, and well ignores arguments if too many }, []);