Re: Function auto-loading
| From: | Rowan Collins | Date: | Tue, 09 Aug 2016 21:26:32 +0000 |
| Subject: | Re: Function auto-loading | ||
| References: | 1 2 3 4 5 6 7 8 9 10 11 12 13 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-94980@lists.php.net to get a copy of this message | ||
On 09/08/2016 18:44, Rasmus Schultz wrote:
On Tue, Aug 9, 2016 at 6:17 PM, Rowan Collins<rowan.collins@gmail.com> wrote:Sure, there is a small advantage over raw require statements, because you're wrapping the file-location logic in a function, which can be a bit more flexible. But that doesn't actually need the engine to do anything special: Proposed: function autoload_function($name) {Ah, I see. The problem with that is, you still have to explicitly list every function you're going to use, so you might as well just use require_once, or call "load_function('substr')" directly. Well, no - there is still the issue of file names and paths becoming a dependency, besides issue of having to locate the file in the first place.In other words, calls to substr() does not trigger the resolve - a statement like "use function substr" on the other hand, does.
require_once FUNCTION_ROOT . str_replace('\', PATH_SEPARATOR, $name) . '.php';
}
register_function_autoloader('autoload_function');
use function Acme\Util\foo; // calls autoload_function('Acme\Util\foo');
use function Acme\Util\bar; // calls autoload_function('Acme\Util\bar');
Current PHP:
function load_function($name) {
if function_exists($name) return;
require_once FUNCTION_ROOT . str_replace('\', PATH_SEPARATOR, $name) . '.php';
}
load_function('Acme\Util\foo');
load_function('Acme\Util\bar');
All we'd really gain is a standard "name" for load_function (i.e. the "use" keyword), and I guess a built-in registry for chaining multiple callbacks together when you run it.
Regards,
--
Rowan Collins
[IMSoP]