# [RFC] Function autoloading through spl_autoload

- Thread: https://externals.io/message/54601
- Original archive: https://news-web.php.net/php.internals/54601
- Started by Rasmus Schultz on 2011-08-14T22:11:07+00:00
- Messages: 3
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #54601 — Rasmus Schultz — 2011-08-14T22:11:07+00:00

URL: https://externals.io/message/54601#54601

Instead of trying to figure out how to autoload functions and cover all your
bases, how about simply adding a __call() magic method?

That way, each application (or framework) can make their own decisions about
what they're going to autoload and how.

A much simpler solution, and one with more potential uses than just
autoloading.

The __call() method could be invoked first in the current namespace, if
present - then in the parent namespace, if present, etc. up to the global
namespace. Since functions are no longer just global, but can now exist in a
namespace, it is likely you're going to need a different "autoloader" or
other dynamic function resolvers in each namespace, since we tend to create
a namespace for classes/functions designed to perform a specific kind of
task - for example, a template/view-engine probably needs to autoload
display-formatting functions, while a controller/dispatcher might need to
autoload action-filters, etc.

Just a thought :-)

/ Rasmus Schultz

---

## #54602 — Ferenc Kovacs — 2011-08-14T22:44:25+00:00

URL: https://externals.io/message/54601#54602 · In reply to: #54601

> Instead of trying to figure out how to autoload functions and cover all your
> bases, how about simply adding a __call() magic method?
>
> That way, each application (or framework) can make their own decisions about
> what they're going to autoload and how.
>
> A much simpler solution, and one with more potential uses than just
> autoloading.
>
> The __call() method could be invoked first in the current namespace, if
> present - then in the parent namespace, if present, etc. up to the global
> namespace. Since functions are no longer just global, but can now exist in a
> namespace, it is likely you're going to need a different "autoloader" or
> other dynamic function resolvers in each namespace, since we tend to create
> a namespace for classes/functions designed to perform a specific kind of
> task - for example, a template/view-engine probably needs to autoload
> display-formatting functions, while a controller/dispatcher might need to
> autoload action-filters, etc.
>
> Just a thought :-)
>
> / Rasmus Schultz
>

we already have a magic method with the name __call.
I'm not sure where you propose adding this magic method, but judging
from it's name, you would like to propose something similar than
__autoload what we have abadoned for many reasons, the main reasons is
that the spl_autoload_register is more explicit and supports having
multiple callbacks.

please create an RFC for your idea, from first glance, I feel that you
didn't really thought over the details yet.


-- 
Ferenc Kovács
@Tyr43l - http://tyrael.hu

---

## #54627 — Rasmus Schultz — 2011-08-15T12:33:55+00:00

URL: https://externals.io/message/54601#54627 · In reply to: #54602

I'm not talking about the __call() magic method, I'm proposing a global
and/or namespaced call to a flat __call() *function *- you could use it to
implement autoloading like so:
*
*
<?php

function __call($name, $arguments)
{
  $path = str_replace('\\', DIRECTORY_SEPARATOR, $name);
  require FUNC_PATH . `DIRECTORY_SEPARATOR` . $path;
  return call_user_func_array($name, $arguments);
}
*
*Since the introduction of namespaces, functions are really more like
"static methods" of the namespace; they're no different from static methods
of a class, really.

If you wanted to autoload methods in class for some reason, you can already
do that - using the __call() magic method, a method-table (array where
method-name => closure) and loading scripts that return an anonymous
function.

Similarly, we could use a namespace-specific __call() function as shown
above, to autoload functions.

It was just a thought - just throwing it out there for discussion, I'm not
submitting a complete RFC at this point :-)

/ Rasmus Schultz




> On Mon, Aug 15, 2011 at 12:11 AM, Rasmus Schultz <rasmus@mindplay.dk>
> wrote:
> > […]
> your
> > […]
> about
> > […]
> in a
> > […]
> create
> > […]
>
> we already have a magic method with the name __call.
> I'm not sure where you propose adding this magic method, but judging
> from it's name, you would like to propose something similar than
> __autoload what we have abadoned for many reasons, the main reasons is
> that the spl_autoload_register is more explicit and supports having
> multiple callbacks.
>
> please create an RFC for your idea, from first glance, I feel that you
> didn't really thought over the details yet.
>
>
> --
> Ferenc Kovács
> @Tyr43l - http://tyrael.hu
